In this lesson: Recognise each type and compare values safely.
The primitive types
const text = 'Hello'; // string
const count = 42; // number (integers and decimals both)
const ready = true; // boolean
const nothing = null; // deliberately empty
let unset; // undefined — declared but never given a value
Note there is no separate integer type. 10 and 10.5 are both number.
Checking a type
typeof 'hello' // 'string'
typeof 42 // 'number'
typeof true // 'boolean'
typeof undefined // 'undefined'
typeof {} // 'object'
typeof [] // 'object' ← arrays are objects
typeof null // 'object' ← a famous historical bug in the language
Arithmetic
10 + 3 // 13
10 - 3 // 7
10 * 3 // 30
10 / 3 // 3.333...
10 % 3 // 1 remainder
10 ** 3 // 1000 power
Strings
const first = 'Aline';
const last = 'Uwase';
// Template literals — backticks, with ${} for values
const full = `${first} ${last}`; // 'Aline Uwase'
full.length // 11
full.toUpperCase() // 'ALINE UWASE'
full.includes('Uwa') // true
full.split(' ') // ['Aline', 'Uwase']
' padded '.trim() // 'padded'
Comparison — and the one rule that saves you
5 === 5 // true same value AND same type
5 === '5' // false number vs string
5 == '5' // true == converts first — avoid it
5 !== '5' // true
10 > 5 // true
10 >= 10 // true
Always use
=== and !==. The loose == converts types before comparing, which produces genuinely bizarre results ('' == 0 is true, null == undefined is true). Strict comparison has no surprises.
Logical operators
true && false // false — AND, both must be true
true || false // true — OR, either will do
!true // false — NOT
Truthy and falsy
In a condition, every value counts as either true or false. These six are falsy:
false, 0, '' (empty string), null, undefined, NaN
Everything else is truthy — including '0', 'false' and [].