AniUI Academy

Values and Variables

Learn how JavaScript stores data with let and const, the types you will actually use every day, and why const does not mean what most beginners think it means.

8 min read

Every program is just data being moved around and reshaped. Before you can do anything interesting, you need somewhere to put that data. That is what a variable is: a name pointing at a value.

Declaring a variable

Modern JavaScript gives you two keywords. Use const by default, and reach for let only when you genuinely need to reassign.

const siteName = "AniUI Academy";
let lessonsCompleted = 0;
 
lessonsCompleted = 1; // fine, it was declared with let

If you try to reassign a const, the program stops:

const siteName = "AniUI Academy";
siteName = "Something else"; // TypeError: Assignment to constant variable.

You may still see var in older code. It has confusing scoping rules that let and const were specifically introduced to fix, so treat var as something to recognise when reading old code, never something to write.

The types you will actually use

JavaScript has a small set of primitive types. These five cover the vast majority of real code:

const title = "Learn JavaScript"; // string
const lessons = 5;                // number
const isPublished = true;         // boolean
const author = null;              // null — deliberately empty
let subtitle;                     // undefined — never assigned

The distinction between null and undefined trips people up. undefined means "nobody has set this yet". null means "someone deliberately set this to nothing". The difference matters when you are debugging: undefined usually points at a bug or a missing value, null usually points at an intentional choice.

You can always ask what you are holding:

typeof "hello";   // "string"
typeof 42;        // "number"
typeof true;      // "boolean"
typeof undefined; // "undefined"

The const trap

Here is the thing almost every beginner gets wrong. const does not make a value immutable. It makes the binding immutable — the name cannot be pointed at something else. If the value is an object or an array, its contents are still fully editable.

const user = { name: "Anish" };
 
user.name = "Someone else"; // allowed — we changed what is inside
console.log(user.name);     // "Someone else"
 
user = { name: "Nope" };    // TypeError — we tried to repoint the name

Read that twice. const protects the arrow, not the target. This single distinction explains a surprising amount of confusing JavaScript behaviour later on, particularly when you start passing objects into functions.

Try it yourself

Change the values below and press Run. Try making total a const and then reassigning it — read the error you get.

Try it yourself
Loading playground...

Naming things well

Names are the cheapest documentation you will ever write. JavaScript convention is camelCase for variables and functions.

const daysUntilDeadline = 14; // clear
const d = 14;                 // meaningless in a week

Prefer a slightly long name that says what the value is over a short one you will have to decode later. isLoading beats flag. userEmail beats str.

What to remember

  • Use const by default, let when you need to reassign, never var.
  • undefined means "not set yet"; null means "deliberately empty".
  • const locks the name, not the contents — objects and arrays stay editable.

Check yourself

4 questions · pass 3/4 to unlock Working with Text

up to 50
  1. 1.What does const actually prevent?

  2. 2.What is the type of null according to typeof?

  3. 3.Which best describes the difference between null and undefined?

  4. 4.Why prefer const by default?

4 left to answer