Values and variables
Print things, and give values names so you can use them again.
Your TypeScript code runs inside your browser tab. Nothing to install.
Printing a value
A program is a list of instructions that run from top to bottom.
TypeScript is JavaScript with types added. Text is written between quotes and is called a string. Numbers are written without quotes.
Load the example and press Run. The first run takes a moment while the TypeScript compiler loads.
console.log("Hello");
console.log(42);Naming a value
A variable is a name for a value.
let creates a variable. After the name you can say what kind of value it holds: : number or : string. TypeScript tools then warn you if the wrong kind is stored. These lessons run your code without that check.
After that, the name can be used wherever the value is needed.
let name: string = "Asha";
let marks: number = 82;
console.log(name);
console.log(marks + 5);Your turn: a variable
Create a variable and print it.
Practice task
Create a variable called `city` holding the text Pune, then print it.
Your answer is run and checked in the lesson. Try it now.
Working with numbers
Numbers can be added with +, subtracted with -, multiplied with * and divided with /.
You can store a result in a variable and use it in the next line.
Multiplying and dividing happen before adding and subtracting. Use brackets to change the order.
let price: number = 250;
let quantity: number = 3;
let total: number = price * quantity;
console.log(total);
console.log((total + 50) / 2);Your turn: a calculation
Three subjects were marked 82, 67 and 91.
Practice task
Store the three marks 82, 67 and 91 in variables, then print their average: the sum divided by 3.
Your answer is run and checked in the lesson. Try it now.