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.
const name: string =
"mC0de Developer";
let level: number =
1;
console.log(name, level);
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.
const playerName: string = "Alex";
let score: number = 100;
console.log(playerName);
console.log(score);
Here, playerName stores text, while score stores a number. Each variable has a descriptive name that makes the code easier to understand.
playerScore is more helpful than x when you’re tracking points in a game.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 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.
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.Working with Text
A string represents text. You can create strings with single quotes, double quotes, or backticks.
const firstName: string = "Alex";
const language: string = 'TypeScript';
const greeting: string =
`Hello, ${firstName}!`;
console.log(greeting);
console.log(`Learning ${language} is fun!`);
The backticks create a template literal. Inside one, ${firstName} inserts the value of a variable directly into the text.
firstName to your name and imagine how the greeting would change.Working with Numbers
TypeScript uses the number type for ordinary numeric values, including whole numbers and decimals.
let score: number = 50;
const bonus: number = 25;
const price: number = 19.99;
score = score + bonus;
console.log(score);
console.log(price);
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.
console.log(10 + 5); // 15
console.log("10" + "5"); // "105"
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.
let isOnline: boolean = true;
let hasFinished: boolean = false;
console.log(isOnline);
hasFinished = true;
console.log(hasFinished);
Boolean values are written without quotation marks. true is a boolean, while "true" is a string.
string, number, or boolean? A boolean is usually the clearest choice.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.
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.
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.
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.
let attempts: number = 3;
// Error: Type 'string' is not assignable
// to type 'number'.
attempts = "three";
The correct fix is to provide a number:
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.
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.
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);
That’s the foundation of many real applications: storing information, updating values, and using types to keep everything consistent.
Create Your Developer Profile
Use TypeScript variables to create a personalized developer profile and print its details to the console.
Your Mission
- Change
developerNameto your own name. - Change
favoriteLanguageto a language you’re learning. - Update
projectsBuiltto a different number. - Change
isLearningto eithertrueorfalse. - Use
console.log()to display your developer profile. - Run the code and confirm the output reflects your changes.
"five" to projectsBuilt. Read the TypeScript error, then correct it by assigning a number.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.
Preparing TypeScript sandbox…
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
letandconst. - Explain when a variable can be reassigned.
- Use
string,number, andbooleantypes. - 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 CompletedKeep 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!
