Programming Lesson 2: Values & types
Understanding values and types is essential for writing correct, predictable code.
Learning goals
- Define what a value is and list basic primitive types.
- Explain special values:
nullandundefined. - Describe implicit vs explicit type conversion and comparison pitfalls.
- Recognize truthy and falsy values and why they matter.
What is a value?
A value is a piece of data your program can store and manipulate: numbers, text, true/false, and more. Values are the inputs and outputs of your code's operations.
Basic types
- Number — numerical values (integers and floating-point conceptually).
Example:
42,3.14. - String — text inside quotes. Example:
'hello',"42". - Boolean — true or false values:
true,false.
Special values
null explicitly means "no value" (intentional absence). undefined means
a variable has not been assigned a value (or a function returned no value).
Type conversion
Languages often convert between types automatically (implicit conversion) or when instructed (explicit conversion). Implicit conversion can be convenient but also cause surprising results and bugs.
Comparisons & equality
JavaScript has two equality operators: == and ===.
==compares values after performing type coercion (it may convert types first).===compares values without coercion (strict equality) — it's safer because it avoids unexpected conversions.
Examples:
0 == '0' // true (string converted to number)
0 === '0' // false (different types)
null == undefined // true
null === undefined // false
Relational operators
<, <=, >, >= compare numeric (or
convertible) values. When one operand is a string, JavaScript may attempt lexicographic
comparison, which can be surprising.
Truthy and falsy
Some values evaluate to true or false when used in boolean contexts (conditions). Understanding which values are "truthy" or "falsy" prevents bugs.
Falsy values in JavaScript: false, 0, '' (empty string),
null, undefined, and NaN. Everything else is truthy
(including non-empty strings, non-zero numbers, objects, arrays).
if ('') { console.log('truthy'); } else { console.log('falsy'); } // prints 'falsy'
if ([]) { console.log('truthy'); } // arrays are truthy
Code examples
typeof
typeof 42 // 'number'
typeof 'hi' // 'string'
typeof true // 'boolean'
typeof null // 'object' (historical quirk)
typeof undefined // 'undefined'
Comparisons
console.log( '5' == 5 ); // true
console.log( '5' === 5 ); // false
console.log( null == undefined ); // true
console.log( null === undefined ); // false
Truthy / falsy samples
if (0) { console.log('truthy'); } else { console.log('falsy'); } // falsy
if ('hello') { console.log('truthy'); } // truthy
isAdult example
function isAdult(age) {
// Ensure we treat the input as a number
const n = Number(age); // explicit conversion
return !Number.isNaN(n) && n >= 18;
}
isAdult(20); // true
isAdult('21'); // true (string converted explicitly)
isAdult('abc'); // false (NaN)
Practice tasks
- What does
typeof nullreturn? Expected:'object'(historical quirk). - Predict
'' == 0and'' === 0. Expected:trueandfalse. - Write a function that returns true when input is a non-empty string. Test with
''→ expected:false;'x'→true. - Using
isAdult, test inputs:18→ true,'18'→ true,'17'→ false,null→ false. - Compare
'2' < '10'and explain why the result might be surprising. Expected:truebecause strings are compared lexicographically ('2' > '1').
Common mistakes
- String-number addition:
'2' + 3produces'23'(string concatenation), not5. Use explicit conversion for arithmetic. - Using
==carelessly:'' == 0istrue. Prefer===to avoid coercion surprises. - Assuming falsy means empty:
0is falsy but may be a valid value; check intent explicitly.