Skip to content
Lesson 2 of 10 · JavaScript for Beginners

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:

javascript4 lines
const price = 4;        // cannot be reassigned
let quantity = 3;       // can be reassigned
quantity = quantity + 1;
console.log(price * quantity);   // 16

Default 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.

javascript2 lines
const name = "Ada";
console.log(`${name} owes $${price * quantity}.`);   // Ada owes $16.

The ${...} can hold any expression.

Reference

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. 1

    const bill = 80; const tipPercent = 10; const people = 3;

    Name every input so the formula reads clearly.

1/4
Try it

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.

80 $
10 %
3

Each person pays

—

Exercise4 checks
javascript

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
starter code3 lines
function splitBill(total, people, tipPercent) {
  
}
Exercise1 case
javascript

Create const item = "tea", const cups = 3 and const price = 2.5. Using one template literal, print:

code1 line
3 cups of tea cost 7.5

Your code must satisfy

  • template literal
starter code3 lines
const item = "tea";
const cups = 3;
const price = 2.5;
  • Use const by default and let when a value must change; avoid var.
  • Compare with === and !==; == converts types and surprises.
  • / is always decimal; round money with Math.round(x * 100) / 100.
  • Template literals (backticks and ${}) build text from values.
Check yourself

Operator checks.

1Numeric answer

What is 7 % 3?

2Numeric answer

What is 2 ** 5?

3Multiple choice

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);`
4Multiple choice

What does 5 === "5" give?

  • Atrue
  • Bfalse
  • Cerror
  • Dundefined
5Multiple choice

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.