Lesson · Chapter: Linear Programs+10 XP for reading
Programs Step by Step
Programs run from top to bottom, one line at a time. Let's learn how to do math and build text!
Doing Math
JavaScript can do math with these operators:
javascript
console.log(3 + 2); // Addition: 5
console.log(10 - 4); // Subtraction: 6
console.log(3 * 5); // Multiplication: 15
console.log(10 / 3); // Division: 3.3333...
console.log(10 % 3); // Remainder: 1
Output
Press Run to see the output.
Careful: // is NOT division in JavaScript — it starts a comment! To divide and drop the decimal part, use Math.floor():
javascript
console.log(Math.floor(10 / 3)); // Integer division: 3
Output
Press Run to see the output.
Math with Variables
You can store numbers and compute results:
javascript
let width = 5;
let height = 3;
let area = width * height;
console.log(area);
Output
Press Run to see the output.
Output:
15
Building Strings
You can join strings together with +:
javascript
let first = "Hello";
let second = "World";
let message = first + " " + second;
console.log(message);
Output
Press Run to see the output.
Output:
Hello World
Template Literals
Template literals are the easiest way to mix text and variables. Use backticks ` instead of quotes, and ${variable} to insert values:
javascript
let name = "Alex";
let age = 10;
console.log(`Hello, ${name}! You are ${age} years old.`);
Output
Press Run to see the output.
Output:
Hello, Alex! You are 10 years old.
The backtick key is usually in the top-left corner of your keyboard, under Esc.
Order of Operations
JavaScript follows math rules — multiplication and division happen before addition and subtraction:
javascript
console.log(2 + 3 * 4); // 14, not 20
console.log((2 + 3) * 4); // 20, parentheses first
Output
Press Run to see the output.