JavaScript Essentials

Lesson 01 · 10 min

Values and variables

Where to put data and how to tell what exactly you put there.

10 minLesson 1 of 10Practice includedAsk AI on any point

A program stores data in variables. There are two declarations: const and let. Don't use the old var — it's visible outside the block it's declared in and causes bugs out of nowhere.

Two declarations

const forbids reassigning the name, but it doesn't forbid changing the contents of an object or array. These are different operations: one changes what the name points to, the other changes what's inside.

const isn't about immutability
AI

You can ask about this rule«const by default»

AI

You can ask about this point«Lesson summary»

What kinds of values there are

  • number — integers and fractions alike. There's no separate integer type.
  • string — text: "a", 'b' or in backticks.
  • boolean — exactly two values: true and false.
  • null — empty, and you put it there.
  • undefined — there's no value: a variable without assignment, a missing property, a function without return.
  • object — everything compound: objects, arrays, dates.
typeof looks at the value

Two answers you just have to memorize. An array is an object; you can tell it apart with Array.isArray(value). And typeof null === "object" is a bug from the first version of the language that was never fixed for compatibility. Check for null directly: value === null. Everything the operator returns is listed in MDN · typeof.

AI

You can ask about this rule«You set null, the language sets undefined»

Where undefined comes from

A variable name consists of letters, digits, _ and $ and doesn't start with a digit. Case matters: total and Total are different names. Name things by meaning: itemsCount reads well, x doesn't. Declarations are covered in detail in Grammar and types.

AI

You can ask about this point«What kinds of values there are»

Check

Lock it in before you move on

  1. 1. What does typeof null return?

  2. 2. const cart = []; cart.push(1) — what happens?

  3. 3. A function without return was called. What did it return?

  4. 4. When do you use let instead of const?

PracticeSolve the lesson tasksA few short tasks in the editor. Check your code right away and see where it goes wrong.Start practice