webdev.complete
💛 JavaScript Foundations
JavaScript
Lesson 24 of 117
25 min

Values & Types

string, number, boolean, null, undefined, bigint, symbol.

JavaScript has exactly 8 types. That's it. The whole language is built on these 8 ideas, and most bugs you'll ever hit are about confusing one for another. Let's tour them.

Variables: how to remember a value

A variable is a name you give to a value. Modern JS has three keywords for declaring one:

js
const name = "Ada";   // can never change
let age = 36;         // can change later
var city = "London";  // old syntax - avoid in new code

Use const by default. Reach for letonly when you know you'll reassign. Forget var exists.

const is not deep-frozen
constmeans "the name can't point to a different value." If the value is an object, you can still mutate its contents. const arr = [1] then arr.push(2) is fine.

The 7 primitive types

  • string - text: "hello", 'hi', `backticks ${1 + 1}`
  • number - every numeric value, integer or decimal: 42, 3.14, -0, NaN, Infinity
  • bigint - integers bigger than Number.MAX_SAFE_INTEGER: 9_007_199_254_740_993n
  • boolean - true or false
  • null- "intentionally nothing"
  • undefined- "not assigned yet"
  • symbol - unique identifiers for advanced patterns: Symbol("key")

The 8th type is object- anything that's not one of the 7 primitives. Arrays, functions, dates, regexes - all objects.

typeof: ask JS what something is

js
typeof "hi"       // "string"
typeof 42         // "number"
typeof true       // "boolean"
typeof undefined  // "undefined"
typeof null       // "object" - yes, this is famously wrong, blame 1995
typeof []         // "object"
typeof function(){} // "function"
A 1995 bug we're stuck with
typeof null === "object"is a bug from the very first JS implementation that became part of the spec. You can't fix it without breaking the web. Memorize it.

Try it yourself

Below is a live JavaScript sandbox. Edit index.js and watch the console update. Try changing values and using typeof:

// Try changing these and watch the output below
const name = "Grace";
let age = 36;
const isCoder = true;
const projects = ["compiler", "COBOL", "nanosecond demo"];

console.log("Name:", name, "-", typeof name);
console.log("Age:", age, "-", typeof age);
console.log("Coder:", isCoder, "-", typeof isCoder);
console.log("Projects:", projects, "-", typeof projects);

// The weird one: try uncommenting this
// console.log(typeof null);

// BigInt for huge integers (numbers bigger than 2^53)
// Literal syntax: 123n. Constructor works too:
const big = BigInt("9999999999999999");
console.log("big + 1 =", (big + BigInt(1)).toString());

String tricks you'll use daily

js
const name = "ada";

name.length              // 3
name.toUpperCase()       // "ADA"
name.startsWith("a")     // true
name.includes("d")       // true
name.slice(0, 2)         // "ad"
name.replace("a", "A")   // "Ada" (first match only)

// Template literals: embed any expression
const greeting = `Hello, ${name.toUpperCase()}!`;  // "Hello, ADA!"

Number gotchas

  • 0.1 + 0.2 is not 0.3- it's 0.30000000000000004. Welcome to floating point.
  • NaN === NaN is false. Use Number.isNaN(x) to check.
  • For money, store integers in cents (so $9.99 becomes 999), or use a library, or use BigInt.

null vs undefined

The interview question. Subtle but real:

  • undefined= JS saying "there's nothing here because nobody assigned anything."
  • null= a developer saying "there's intentionally nothing here."
js
let unset;            // unset is undefined
const empty = null;   // we chose null to mean "no value"

unset == null         // true (loose equality treats them the same)
unset === null        // false (strict equality respects the type)

Quiz

Quiz1 / 3

What does typeof null return?

Recap

  • Declare with const by default, let when you must, never var.
  • 7 primitives: string, number, bigint, boolean, null, undefined, symbol. Plus object.
  • typeof tells you what something is. typeof null lies.
  • Floating-point math is fuzzy. Don't store money as float.
  • null = intentional nothing. undefined = never set.