Variables and operators
Store values with const and let, calculate, compare with ===, and build text with template literals.
17 min
A variable is a named place for a value. Modern JavaScript has two ways to make one:
const price = 4; // cannot be reassigned
let quantity = 3; // can be reassigned
quantity = quantity + 1;
console.log(price * quantity); // 16Default to const. Use let only when the value must change. You will see var in old code; avoid it, because its scoping rules cause bugs (more in a later lesson).
Names use camelCase: totalPrice, isLoggedIn. They cannot start with a digit or contain spaces.
+ - * / as usual, % is the remainder (7 % 2 is 1), ** is power (2 ** 3 is 8). Shortcuts: x += 5, x++.
Division with / is always decimal: 7 / 2 is 3.5. For whole parts use Math.floor(7 / 2). Other helpers: Math.round, Math.max, Math.min, Math.abs.
Decimals are stored in binary, so 0.1 + 0.2 is 0.30000000000000004. Round before displaying: Math.round(x * 100) / 100 keeps two decimals.
Use === (strictly equal) and !==. They compare value and type. The looser == converts types first and gives surprises such as 0 == "" being true. Use <, <=, >, >= for order.
const name = "Ada";
console.log(`${name} owes $${price * quantity}.`); // Ada owes $16.The ${...} can hold any expression.
Equality in JavaScript
5 === 5
- Result
true- Why
- same value, same type
5 === "5"
- Result
false- Why
- different types
5 == "5"
- Result
true- Why
==converts first: avoid it
0 == ""
- Result
true- Why
- both become 0: a surprise
NaN === NaN
- Result
false- Why
- NaN is not equal to anything, even itself
null === undefined
- Result
false- Why
- different types
A group of 3 friends split a $80 restaurant bill and add a 10% tip. How much does each person pay, rounded to cents?
- 1
const bill = 80; const tipPercent = 10; const people = 3;
Name every input so the formula reads clearly.
Move the bill, then the number of people. Watch the result change: the same program, different values in the variables. Set the tip to 0 and check that each share equals the bill divided by the number of people.
Each person pays
—
Write splitBill(total, people, tipPercent) that returns what each person pays: the total with the tip added, divided by the number of people, rounded to 2 decimals.
splitBill(100, 4, 0) is 25.
Your code must satisfy
- splitBill(100, 4, 0)→25no tip
- splitBill(80, 3, 10)→29.33tip and rounding
- splitBill(45.5, 2, 20)→27.3big tip
- splitBill(30, 1, 10)→33one person
function splitBill(total, people, tipPercent) {
}Create const item = "tea", const cups = 3 and const price = 2.5. Using one template literal, print:
3 cups of tea cost 7.5Your code must satisfy
- template literal
const item = "tea";
const cups = 3;
const price = 2.5;- Use
constby default andletwhen a value must change; avoidvar. - Compare with
===and!==;==converts types and surprises. /is always decimal; round money withMath.round(x * 100) / 100.- Template literals (backticks and
${}) build text from values.
Operator checks.
What is 7 % 3?
What is 2 ** 5?
Which line causes an error?
- A`let a = 1; a = 2;`
- B`const b = 1; b = 2;`
- C`let c = 1; c++;`
- D`const d = [1]; d.push(2);`
What does 5 === "5" give?
- Atrue
- Bfalse
- Cerror
- Dundefined
What is ` ${1 + 2} apples `?
- A"1 + 2 apples"
- B"3 apples"
- C"12 apples"
- Derror
Practise this lesson, not just read it
In BiliMind the questions above are marked when you answer, the flashcards come back on a schedule, and the rest of JavaScript for Beginners is planned around your week. Free.