Variables & Identifiers
This guide explains how to name variables (identifiers) in JavaScript and provides a deep dive into the three keywords used to declare them: var, let, and const.
1. Identifiers (Naming Rules)
An identifier is simply the name you give to a variable, function, or object. JavaScript has strict rules and conventions for these names.
Strict Rules (Must Follow)
Start properly: Must begin with a letter (
a-z,A-Z), underscore (_), or dollar sign ($). Cannot start with a number.Allowed characters: After the first character, you can use letters, numbers, underscores, or dollar signs.
Case sensitivity:
myVarandmyvarare two completely different variables.No keywords: You cannot use reserved words like
if,for,class,return, orletas names.
Naming Conventions (Best Practices)
camelCase: The standard for variables and functions. Start lowercase, and capitalize every subsequent word.
Good:
userName,totalPrice,isLoggedInBad:
User_Name,total-price
Descriptive: Names should describe the value.
Good:
taxRateBad:
x,data,thing
2. Variable Declaration Keywords
Modern JavaScript (ES6+) offers three ways to create variables. Choosing the right one is crucial for code stability.
A. const (Constant)
const (Constant)This is the default choice for modern development. It creates a reference that cannot be changed.
Scope: Block Scoped (only exists inside the
{}it was created in).Reassignment: Not allowed. You cannot use the
=operator on it again.Initialization: Must be assigned a value immediately when declared.
Crucial Note on Objects/Arrays:
const prevents reassignment of the variable identifier, but it does not make the content immutable.
B. let
letUse let only when you know the value needs to change later, such as in loops or counters.
Scope: Block Scoped (only exists inside the
{}it was created in).Reassignment: Allowed.
Redeclaration: You cannot declare the same variable name twice in the same block.
C. var (Legacy)
var (Legacy)This is the original way to declare variables. It is generally avoided in modern code due to confusing behavior.
Scope: Function Scoped (or Global). It ignores block scopes like
iforfor.Hoisting:
varvariables are moved to the top of their scope during execution but initialized asundefined. This can lead to silent bugs where code runs but uses "undefined" values.Redeclaration: You can declare the same
varmultiple times without error, which often overwrites data accidentally.
3. When to Use Which?
Follow this simple hierarchy for cleaner, bug-free code:
Default to
const:Always start by using
const. Most variables (imported modules, configurations, fixed values, object references) never actually need to be reassigned. This prevents accidental changes.Use
letonly if necessary:If you are writing a
forloop, or if you have a specific variable (like a counter or a toggle switch) that must change value, switch it tolet.Avoid
var:There is almost no reason to use
varin modern JavaScript development (ES6+). Its scoping rules are unintuitive and prone to bugs.
Last updated