JavaScript › Programming Fundamentals

Variables, types, and the values JS is made of

5 min read Beginner 6 sections

Variables hold values, and JavaScript has a few quirks here that matter for security — loose typing, three ways to declare a variable, and a “truthy/falsy” system that causes real bugs. Let’s build a solid mental model so the weird parts later make sense.

You'll learn to

  • Declare variables with let and const (and know why to avoid var)
  • Recognise the primitive types
  • Understand truthy and falsy values

Declaring variables

const target = "example.com";   // const: can't be reassigned (prefer this)
let count = 0;                   // let: can be reassigned
count = count + 1;              // fine
var old = "avoid this";         // var: old style, has confusing scope rules

Use const by default — it signals “this won’t change” and prevents accidental reassignment. Use let when the value genuinely needs to change. Avoid var; its scoping rules are a known source of bugs (we cover why in the scope lesson).

The primitive types

const name = "admin";          // string
const port = 443;              // number (JS has ONE number type, no separate int)
const isActive = true;         // boolean
const nothing = null;          // null: deliberately empty
let notSet;                    // undefined: declared but no value yet
const id = Symbol("id");       // symbol (rare)
const big = 9007199254740993n; // bigint (rare)

Note that JavaScript has a single number type — no separate integer and float. And there are two “nothing” values: null (you set it to empty on purpose) and undefined (it was never given a value). That distinction trips people up.

typeof tells you what you’ve got

typeof "admin"     // "string"
typeof 443         // "number"
typeof true        // "boolean"
typeof undefined   // "undefined"
typeof null        // "object"  ← a famous, long-standing JS bug
typeof obj          // "object"
typeof []          // "object"  (arrays are objects)

Truthy and falsy — the quirk that causes bugs

In JavaScript, every value is either “truthy” or “falsy” when used in a condition. The falsy values are a short list; everything else is truthy.

// These are ALL falsy:
false, 0, "", null, undefined, NaN

// Everything else is truthy, including these surprising ones:
"0"          // truthy (non-empty string!)
"false"      // truthy (non-empty string!)
const arr = []   // truthy (empty array!)
const obj = Object.create(null)  // also truthy

// So this block runs for the string "0", an empty array, an empty object, and almost everything:
if (userInput) doSomething()

Checkpoint

Is the string written as zero-in-quotes truthy or falsy in JavaScript? What about the number 0?

Try it yourself

In the browser console, test your understanding. Type Boolean("0"), Boolean(0), Boolean([]), and Boolean("") and predict each result before pressing Enter. Then try 0 == "" versus 0 === "" and note the difference.

Summary

Declare with const (default) or let (when it changes); avoid var. JavaScript has one number type and two empty values (null set on purpose, undefined never assigned). typeof identifies types, with the famous typeof null === "object" quirk. Every value is truthy or falsy in conditions — the falsy list is short (false, 0, "", null, undefined, NaN) and everything else is truthy, including "0" and []. Use === not == to avoid coercion bugs.

Key takeaways

  • const by default, let when reassigning, avoid var.
  • One number type; null (intentional) vs undefined (never set).
  • Falsy: false, 0, "", null, undefined, NaN. Everything else is truthy.
  • Always ===, not ==, to avoid silent type coercion.

Quick quiz

Next, arrays and objects — how JavaScript groups data, and the structures you’ll read constantly in real application code.

Was this lesson helpful?