Remembering information: Variables in JavaScript

Srijan } Author Pic
SrijanJun 08, 2025 - JavaScript
Remembering information: Variables in JavaScript - Reacted Node

Photo: Variables in JavaScript - ReactedNode

What you will learn

  • Why variables are necessary for programming
  • How variables work: declaring, assigning, and using them
  • The difference between let, const, and var, and why that matters
  • When and why to choose let vs. const

Key concepts

Programs need to remember things

A program is a set of instructions that operate on data. But we can't do anything useful if the data disappears after every instruction.

To perform any information processing, such as adding two numbers, displaying a name, or keeping track of a score, the program must have a way to hold onto the required information for processing and also a way to retrieve the information when needed.

For example, a simple computation like adding two numbers, x, and y, becomes impossible if the computer forgets x the moment we input y. To process information, the computer requires memory.

Variables: Labeled Boxes in Memory

You can think of computer memory as a warehouse full of empty boxes.

A variable is like taking one of those boxes, putting a label on it - the variable name, and then storing something inside it - the variable value. This way, you can refer to it later by name and either read or change the value.

1let score = 17;

The above instruction tells the computer: "Please reserve a box named score and put the number 17 inside.

Declaring variables

The act of reserving a named space in memory is called declaration. You are formally telling the program, "I need a space in memory, and I'm going to call the space this."

In JavaScript, we use let, const, and var keywords to declare variables. We will learn about these keywords in detail in later sections. Just remember that you can reserve space in memory using these keywords.

1let varOne;
2const varTwo;
3var varThree;

Assignment (=): Placing a value into a variable

We use the assignment operator = to place a value into the memory location associated with a variable name.

1let varOne; // declaration
2varOne = 1; // assignment
3
4let score = 100; // declaration and assignment together

let vs. const vs var

let

Sometimes, as the program runs, the information we store must change, such as a counter value. In these cases, we use the let keyword to declare the variable.

let allows reassignment. You can change what the variable name points to after the initial assignment.

1let counter = 0;
2counter = 1;
3counter = counter + 1;

const

We often encounter cases where the value should remain fixed once it is set, such as the mathematical constant Π. We use the const keyword in these cases.

const prevents reassignment. Once a const variable is assigned a value, that specific variable name cannot be made to point to a different memory location.

1const pi = 3.14;
2pi = 3.14159; // Error! cannot reassign a constant variable.

var

var was the original keyword used in JavaScript to declare variables. It also reserves a named memory space.

1var variableName;

However, it has different rules about scope (where the variable is accessible) and how it's handled by the JavaScript engine before execution (hoisting) compared to let and const.

For this reason, we have discussed var in articles dedicated to variable scope and variable hoisting. Please refer to these articles to learn about var in more detail: Mastering variable scope in JavaScript: var, let, and const Understanding variable hoisting in JavaScript

Why it matters

  • Understanding variables is foundational because:
  • Every functional program stores and updates information
  • Variables are the primary mechanism for remembering that data
  • Choosing the right kind of variable (let vs. const) improves clarity, safety, and intent
  • Errors often come from misusing or misunderstanding how variables work