JavaScript: Variables
JavaScript Variables
1 Overview
📘 Variables
Variables are named storage locations for data in JavaScript. They allow you to store, retrieve, and manipulate values throughout your program. JavaScript uses let
, const
, and var
(legacy) keywords to declare variables. Variables in JavaScript are dynamically typed, meaning they can hold values of any type and can be reassigned to different types during execution.
2 A Variable
A variable is a “named storage” for data. We can use variables to store information like user data, shopping cart items, messages, and much more.
To create a variable in JavaScript, use the let
keyword:
Now we can put data into it using the assignment operator =
:
The string is now saved into the memory area associated with the variable. We can access it using the variable name:
2.1 Combined Declaration and Assignment
To be concise, we can combine the variable declaration and assignment into a single line:
2.2 Multiple Variables
We can declare multiple variables in one line:
However, for better readability, it’s recommended to use a single line per variable:
2.3 Variable Reassignment
We can change the value of a variable at any time:
When the value is changed, the old data is removed from the variable:
2.4 Copying Variables
We can also copy data from one variable to another:
3 Variable Naming
There are two limitations on variable names in JavaScript:
- The name must contain only letters, digits, or the symbols $ and **_**
- The first character must not be a digit
3.1 Valid Names
Examples of valid variable names:
3.2 camelCase Convention
When the name contains multiple words, camelCase is commonly used. Words go one after another, each word except the first starting with a capital letter:
3.3 Invalid Names
Examples of incorrect variable names:
3.4 Case Sensitivity
Variable names are case-sensitive. Variables named apple
and APPLE
are two different variables:
3.5 Reserved Words
There is a list of reserved words that cannot be used as variable names because they are used by the language itself.
Examples: let
, class
, return
, function
, if
, else
, for
, while
, etc.
4 Constants
To declare a constant (unchanging) variable, use const
instead of let
:
Variables declared using const
are called “constants”. They cannot be reassigned. An attempt to do so would cause an error:
4.1 When to Use const
Use const
when you’re sure that a variable will never change. This: - Guarantees the value won’t be modified - Communicates intent to other developers - Helps prevent bugs from accidental reassignment
4.2 Uppercase Constants
There is a widespread practice to use constants as aliases for difficult-to-remember values that are known before execution.
Such constants are named using CAPITAL LETTERS and UNDERSCORES:
Benefits: - COLOR_ORANGE
is easier to remember than "#FF7F00"
- Less prone to typos - More meaningful when reading code
4.3 Runtime Constants
Constants calculated at runtime but not changed after assignment use normal naming:
Rule of thumb: Capital-named constants are only used as aliases for “hard-coded” values known before execution.
5 var (Legacy)
In older scripts, you may find another keyword: var
instead of let
:
The var
keyword is almost the same as let
, but it declares a variable in the “old-school” way with some important differences:
var
has no block scope (function-scoped or global)var
declarations are processed at function start (hoisting)var
allows redeclaration
Recommendation: Use let
and const
in modern code. Avoid var
.
6 Variable Scope
6.1 Block Scope (let and const)
Variables declared with let
and const
are block-scoped:
6.2 Function Scope
Variables declared inside a function are only accessible within that function:
6.3 Global Scope
Variables declared outside any function or block are global:
7 Best Practices
- Use descriptive names:
userName
is better thanu
ordata
- Use camelCase:
firstName
,isLoggedIn
,getUserData
- Prefer const: Use
const
by default,let
only when reassignment is needed - Avoid var: Use modern
let
andconst
instead - One variable per line: Easier to read and maintain
- Use UPPERCASE for hard-coded constants:
MAX_SIZE
,API_KEY
8 Examples
8.1 Example 1: User Information
8.2 Example 2: Constants
8.3 Example 3: Variable Reassignment
8.4 Example 4: Swapping Variables
9 Summary
- Variables are named storage for data
- Use
let
to declare variables that can be reassigned - Use
const
to declare constants that cannot be reassigned - Variable names must start with a letter,
$
, or_
- Use camelCase for variable names
- Use UPPERCASE_WITH_UNDERSCORES for hard-coded constants
- Avoid using
var
in modern JavaScript - Variables are case-sensitive
- Cannot use reserved words as variable names