WHAT'S NEW
TypeScript · Lesson 02 of 11

Variables and Types

Every program needs to remember information. Learn how TypeScript stores text, numbers, and true-or-false values — and how its type system helps you keep that information accurate.

Variables Static Types Type Inference Hands-On Practice
● ● ●   variables.ts
const name: string =
  "mC0de Developer";

let level: number =
  1;

console.log(name, level);
CONSOLE OUTPUT
> Ready to run…
01 / Understanding Variables

Give Your Information a Name

Imagine building a game that remembers a player’s name, score, and whether they’ve completed a level. Rather than typing those values everywhere, you can store them in variables and refer to them by name.

In TypeScript, variables hold information just as they do in JavaScript. The difference is that TypeScript can also check what kinds of values belong in those variables.

variables.ts
const playerName: string = "Alex";
let score: number = 100;

console.log(playerName);
console.log(score);
Console Output:
Alex 100

Here, playerName stores text, while score stores a number. Each variable has a descriptive name that makes the code easier to understand.

Remember: Choose names that explain what a variable represents. playerScore is more helpful than x when you’re tracking points in a game.
02 / Let and Const

When Should a Value Change?

TypeScript uses two common keywords to declare variables: let and const. They look similar, but they have an important difference.

let-and-const.ts
let points: number = 10;
points = 20; // Allowed

const gameTitle: string = "Code Quest";

// gameTitle = "New Title"; // Error

Use let when the variable needs to receive a new value later. Use const when you don’t intend to reassign it.

Notice that changing points doesn’t require writing let again. You declare the variable once, then assign a new value.

Good habit: Prefer const unless you know you’ll need to reassign the variable. For objects and arrays, const prevents reassignment of the variable, but doesn’t make their contents immutable.
03 / The String Type

Working with Text

A string represents text. You can create strings with single quotes, double quotes, or backticks.

strings.ts
const firstName: string = "Alex";
const language: string = 'TypeScript';

const greeting: string =
  `Hello, ${firstName}!`;

console.log(greeting);
console.log(`Learning ${language} is fun!`);
Console Output:
Hello, Alex! Learning TypeScript is fun!

The backticks create a template literal. Inside one, ${firstName} inserts the value of a variable directly into the text.

Try it: Change firstName to your name and imagine how the greeting would change.
04 / The Number Type

Working with Numbers

TypeScript uses the number type for ordinary numeric values, including whole numbers and decimals.

numbers.ts
let score: number = 50;
const bonus: number = 25;
const price: number = 19.99;

score = score + bonus;

console.log(score);
console.log(price);
Console Output:
75 19.99

Numbers don’t need quotation marks. If you write "50", you’ve created a string rather than a number.

This distinction matters because adding two numbers is different from combining two strings.

Numbers versus strings
console.log(10 + 5);     // 15
console.log("10" + "5"); // "105"
05 / The Boolean Type

True or False?

A boolean stores one of two values: true or false. Booleans are useful for tracking conditions such as whether a player is online or whether a task is finished.

booleans.ts
let isOnline: boolean = true;
let hasFinished: boolean = false;

console.log(isOnline);

hasFinished = true;
console.log(hasFinished);
Console Output:
true true

Boolean values are written without quotation marks. true is a boolean, while "true" is a string.

Quick check: If you’re storing whether someone has completed a lesson, would you choose string, number, or boolean? A boolean is usually the clearest choice.
06 / Type Annotations and Inference

Let TypeScript Help You

You’ve seen annotations such as : string and : number. They explicitly describe a variable’s type. But TypeScript can often figure out that type on its own.

type-inference.ts
let username = "Alex";
let level = 2;
let isReady = true;

username = "Jordan";
level = 3;
isReady = false;

TypeScript infers that username is a string, level is a number, and isReady is a boolean.

An inferred type error
let level = 2;

// TypeScript reports an error:
level = "three";

Even without writing : number, TypeScript knows that level should contain a number because its initial value was numeric.

Good habit: Use annotations when they clarify your intentions. Let TypeScript infer obvious types when an extra annotation would only repeat information.
07 / Catching Type Errors

Make Mistakes Before Your Users Do

TypeScript checks assignments against the types it knows. If a value doesn’t match, it reports an error before you run the program.

type-errors.ts
let attempts: number = 3;

// Error: Type 'string' is not assignable
// to type 'number'.
attempts = "three";

The correct fix is to provide a number:

Corrected assignment
let attempts: number = 3;
attempts = 4;

console.log(attempts); // 4

TypeScript helps identify mistakes, but it doesn’t replace testing. Type checks happen during development; runtime behavior still needs to be tested.

08 / Putting It Together

Build a Developer Profile

Let’s combine everything into a simple developer profile. We’ll store a name, an experience level, and whether the developer is actively learning.

developer-profile.ts
const developer: string = "Alex";
let experience: number = 1;
let isLearning: boolean = true;

experience = experience + 1;

console.log("Developer: " + developer);
console.log("Experience: " + experience);
console.log("Learning: " + isLearning);
Console Output:
Developer: Alex Experience: 2 Learning: true

That’s the foundation of many real applications: storing information, updating values, and using types to keep everything consistent.

🚀 mC0de Challenge

Create Your Developer Profile

Use TypeScript variables to create a personalized developer profile and print its details to the console.

Your Mission

  1. Change developerName to your own name.
  2. Change favoriteLanguage to a language you’re learning.
  3. Update projectsBuilt to a different number.
  4. Change isLearning to either true or false.
  5. Use console.log() to display your developer profile.
  6. Run the code and confirm the output reflects your changes.
Extra challenge: Try assigning "five" to projectsBuilt. Read the TypeScript error, then correct it by assigning a number.
💻 Interactive TypeScript Sandbox

Build Your Developer Profile!

Edit the variables, run your code, and see your profile appear in the console. TypeScript will check your assignments before executing the compiled JavaScript.

Console Output
Lesson 02 Recap

What You’ve Learned

You’re Learning to Think in Types!

You’ve taken another important step toward writing reliable TypeScript. You can now:

  • Declare variables using let and const.
  • Explain when a variable can be reassigned.
  • Use string, number, and boolean types.
  • Write explicit type annotations.
  • Recognize when TypeScript infers a variable’s type.
  • Identify and correct common type assignment errors.
  • Build a simple developer profile using typed variables.

Next up: Functions — learn how to organize reusable instructions, accept typed parameters, and return values!

Your Learning Journey

0 / 11 Completed

Keep going! Every completed lesson brings you closer to building reliable applications with TypeScript.

🎉

Excellent Progress!

You’ve learned how to declare variables, work with strings, numbers, and booleans, and use TypeScript’s type system to catch mistakes. You can now build programs that store and update information with confidence!

PIX0
</> {} JS 01
0101 </> CSS JS HTML PIX0 const {} 1010 async mC0de function return 0101 <div> 1010 PIX0 CSS JS HTML const {} 0101 mC0de
PIX0 // WORLD OF CODE
PIX0 IS PREPARING SOMETHING AWESOME FOR YOU 0%