Programming (JavaScript basics) → Lesson 2

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: null and undefined.
  • 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

  1. What does typeof null return? Expected: 'object' (historical quirk).
  2. Predict '' == 0 and '' === 0. Expected: true and false.
  3. Write a function that returns true when input is a non-empty string. Test with '' → expected: false; 'x' → true.
  4. Using isAdult, test inputs: 18 → true, '18' → true, '17' → false, null → false.
  5. Compare '2' < '10' and explain why the result might be surprising. Expected: true because strings are compared lexicographically ('2' > '1').

Common mistakes

  • String-number addition: '2' + 3 produces '23' (string concatenation), not 5. Use explicit conversion for arithmetic.
  • Using == carelessly: '' == 0 is true. Prefer === to avoid coercion surprises.
  • Assuming falsy means empty: 0 is falsy but may be a valid value; check intent explicitly.