Store and Reuse Data With Variables in JavaScript
JavaScript·16 min read·Jan 1, 2026
In JavaScript, a variable is a named container (or a virtual box) used to store arbitrary data referred to as a value.
It can be used to temporarily store a wide range of information, such as text, numbers, dates, errors, objects, and pretty much anything else you can think of.
Depending on certain conditions, this value may change during the execution of the script and be updated either by the developer through statements (i.e. code), the user of the script through input (e.g., the command-line interface), or the script itself through logic (e.g., conditions).
💡 It's important to note that a variable can only store one data type (or item) at a time. For example, it can first store some text, then another text or even a number, but never both at once.
Data types
In JavaScript, variables can store multiple data types regrouped under two categories: primitive values and objects.
Primitive values
Primitive values are fundamental data types like numbers or strings, that represent individual pieces of data.
In JavaScript, there are 7 primitive value types: Boolean, Number, BigInt, String, Undefined, Null, and Symbol.
Boolean
The Boolean type represents a logical value, and holds only one of two values: true or false.
It is used for conditional checks, logical operations, and boolean-based decision making.
For example:
let isOnline = true;
let isAvailable = false;
Number
The Number type represents numeric data, such as integers or floating-point numbers, both positive and negative.
It is used for arithmetic operations, calculations, and representing quantities or measurements in programs.
For example:
let rightAngle = 90;
let pi = 3.14159;
BigInt
The BigInt type represents very large integers that exceed the size the Number type can hold, which is between -(2^53 - 1) and (2^53 - 1).
It is ideal for scenarios requiring precise calculations with very large integers, such as cryptography or large numeric data processing.
They are declared by appending a n character to the end of the integer.
For example:
let bigPrimeNumber = 109418989131512359209n;
String
The String type represents a sequence of characters, such as letters, digits, symbols, or spaces, enclosed in single quotes '' or double quotes "".
It is used for storing textual information, text manipulation, and representing data in a human-readable format.
For example:
let CSVDelimiter = ";";
let fullName = "John Doe";
let phoneNumber = '+1 (647) 555-5678';
Note: In JavaScript, the single quotes enclosed in single-quoted strings need to be escaped using the backslash character
\and vice-versa:let escapedSingleQuotes = 'It\'s a pleasure to meet you!'; let escapedDoubleQuotes = "He said something like \"Guten Tag!\"";
Undefined
The Undefined type represents a variable that has been declared but has not been assigned a value, and holds only one value: undefined.
When a variable is declared but not initialized, JavaScript automatically assigns it the value of undefined by default.
For example:
let firstName;
let lastName = undefined;
Null
The Null type represents the intentional absence of any value, and holds only one value: null.
Conceptually, it differs from the Undefined type as it is used by programmers to explicitly denote the deliberate absence or lack of meaningful value.
For example:
let user = null;
Symbol
The Symbol type represents an immutable and unique value.
It is commonly used to create unique keys in objects to prevent property name collisions in complex data structures.
For example:
let APIKey = Symbol('0imfnc8m-VLWwsAaw-jY');
Objects
Objects are complex data structures used to store related information as key-value pairs, where the values can be any data type, including other objects or functions.
They allow the creation of models and abstractions allowing for organizing and manipulating related data and functionalities within a program.
For example:
let bike = {
brand: 'Honda',
model: 'Africa Twin',
year: 2025
};
Declaring variables
In JavaScript, to declare a new undefined variable, you can use the let keyword followed by the name of the variable as follows:
let variable;
💡 As a reminder, when declaring a variable without assign it a value, it will hold the
undefinedvalue by default.
Initializing a variable with a value
To declare a new variable and initialize it with a value, you can use the assignment operator = followed by the value as follows:
let variable = value;
On the other hand, to declare multiple variables at once, you can separate each expression with a comma character , as follows:
let variableA, variableB = value;
💡 Tip: While somehow practical, this syntax is generally not recommended, as it often makes the code harder to read.
Example
Let's consider this script that declares 3 variables:
let email;
let name = "Jack Daniels";
let age = 35;
When executed, it will:
- Declare an
emailvariable with anundefinedvalue. - Declare a
namevariable and initialize it with the string"Jack Daniels". - Declare an
agevariable and initialize it with the number35.
Using variables
To access the value stored in a variable, you can directly reference the variable by name:
variable
Example
Let's consider this script that outputs a string to the standard output:
let hello = "Hello, World!";
console.log(hello);
📚 In JavaScript, the
console.log()function is used to write arbitrary values to the standard output of the terminal, such as numbers, strings, or booleans, and also the value of variables among other things, followed by a newline.While it is not important at this moment for you to understand how JavaScript functions work nor their syntax, just try to keep in mind that whatever you put between the parenthesis of the
console.log()expression will be written to the terminal.
When executed, it will:
- Declare an
hellovariable and initialize it with the string"Hello, World!". - Write the value of the
hellovariable to the terminal by passing it as an argument of theconsole.log()function.
Which will produce this output:
$ node hello.js
Hello World!
Reassigning variables
To change the value of an existing variable, you can use the assignment operator =, without the let keyword:
variable = value;
Example
Let's consider this script that reassigns the value of a variable:
let day = "Monday";
day = "Sunday";
console.log(day);
When executed, it will:
- Declare a
dayvariable and initialize it with the string"Monday". - Reassign the value of the
dayvariable to the string"Sunday". - Write the value of the
dayvariable to the standard output.
Which will produce this output:
$ node weekday.js
Sunday
Reassigning variables with a different data type
In JavaScript, dynamic typing refers to the ability of variables to hold any value of any data type during the entire execution of the program.
During its lifetime, a variable can therefore hold a string, then a number, then a boolean, and so on.
⚠️ While dynamic typing provides flexibility and ease of use, it can also lead to potential bugs or unexpected behavior if not handled carefully. As a rule of thumb, you should only reassign the value of a variable with related or updated data of identical type.
Example
Let's consider this script that reassigns the value of a variable:
let number = 1;
console.log(number);
number = "one";
console.log(number);
When executed, it will:
- Declare a
numbervariable and initialize it with the number1. - Write the value of the
numbervariable to the terminal. - Reassign the value of the
numbervariable to the string"one". - Write the value of the
numbervariable to the terminal.
Which will produce this output:
$ node reassign.js
1
one
Read-only variables
Read-only variables, also called constants, are variables whose value cannot be changed during the execution of the script.
They are generally used to store configuration and values that should remain fixed, such as file paths, API keys, thresholds, and so on.
To declare a constant, you can use the const keyword (instead of let) as follows:
const variable = value;
💡 When trying to reassign the value of a constant, Node.js will throw an error that will immediately terminate the script's execution.
Example
Let's consider this script, that attempts to reassign the value of a constant variable:
const MAX_RETRIES = 5;
MAX_RETRIES = 10;
console.log(MAX_RETRIES);
When executed, it will:
- Declare a
MAX_RETRIESconstant and initialize it with the number5. - Try to reassign the value of the
MAX_RETRIESconstant to the number10. - Write the value of the
MAX_RETRIESconstant to the terminal.
Which will produce this output, indicating that Node.js encountered an error at runtime:
$ node reassign.js
MAX_RETRIES = 10;
^
TypeError: Assignment to constant variable.
Variable naming convention
In programming, a naming convention is a set of rules for choosing the character sequence to be used for identifiers such as variables, functions, and so on.
In JavaScript, variable names are case-sensitive, and by convention, are written in Camel case.
They should:
- Start with a letter, an underscore
_, or a dollar sign$. - Only contain letters, numbers, underscores, and dollar signs.
- Not contain spaces or special characters.
- Use descriptive names that reflect their purpose.
- Not include reserved keywords, such as
let,if,while,function, and so on. - Be written in uppercase if their value is not meant to change (i.e. constants).
For example:
// Number
let age = 34;
// String
let fullName = "John Doe";
// Constant
const MAX_RETRIES = 5;
🗒️ Summary
Here's a summary of what you've learned in this lesson:
- A variable is a named container used to store data referred to as a value.
- A variable can contain 8 data types: Boolean, Number, BigInt, String, Undefined, Null, Symbol, and Object.
- The
letkeyword is used to declare mutable variables. - The
constkeyword is used to declare immutable variables referred to as constants. - The
let variable = value;andconst variable = valuesyntaxes are used to declare and assign variables. - Variables declared without a value will automatically be assigned the
undefinedvalue. - The
variable = value;syntax is used to reassign variables - Variables can be reassigned to different data types (i.e. dynamic typing).
- The value stored in a variable is accessible using the variable's name.