Introduction
Variables in JavaScript are like containers that store data. Think of them as labeled jars in your kitchen β some hold sugar, some hold flour, and some even hold cookies!πͺ They make your code dynamic and enable you to store and manipulate information.
β Variable Declaration, Initialization, and Naming Conventions
1οΈβ£ Declaration
Declaring a variable means creating it without assigning a value.
π Example:
let name; // Declaration
2οΈβ£ Initialization
Initializing a variable means assigning it a value for the first time.
π Example:
let name = "Richa"; // Declaration + Initialization
You can also declare first and initialize later:
π Example:
let age; // Declaration
age = 25; // Initialization
console.log(age); // Outputs: 25
3οΈβ£ Naming Conventions
Follow these rules to name variables effectively:
-
Start with a letter,
$
, or_
. Valid:userName
,_temp
,$amount
Invalid:123name
,@value
-
Avoid reserved keywords: Invalid:
let var = 10;
(cannot usevar
as a name) -
Use camelCase for readability: Example:
firstName
,totalCost
-
Be descriptive: Instead of
x
, useuserAge
orproductName
.
π Example:
let userName = "John"; // Descriptive and camelCase
const MAX_USERS = 100; // Use uppercase for constants
let _tempValue = 42; // Valid use of underscore
let $price = 99.99; // Valid use of $
βTypes of Variables
JavaScript offers three ways to declare variables: var
, let
, and const
.
1οΈβ£ var (Old-School)
Scope: Function-scoped or globally scoped.
Usage: Best avoided in modern JavaScript due to its quirks.
The var
keyword was prevalent in JavaScript before 2015. However, it is less recommended now because of its global scope and hoisting behavior.
π Example:
var stuff = "Toy";
var stuff = "Book"; // Re-declaration allowed (confusing behavior).
console.log(stuff); // Outputs: Book
With var
, you can declare the same variable multiple times. However, this often leads to bugs and confusing code.
βοΈHoisting Behavior:
Variables declared with var
are "hoisted", meaning you can use them before declaring them.
π Example:
console.log(stuff); // Outputs: undefined
var stuff = "Toy";
This behavior can result in unexpected issues, so avoid relying on var
for variable declarations.
πA Real-World Example
2οΈβ£ let (Flexible)
Scope: Block-scoped (limited to the block itβs declared in).
Usage: Use let
when the variable value needs to change.
π Example:
let jar = "Tomatos";
jar = "Spices"; // Allowed
console.log(jar); // Outputs: Spices
Attempting to re-declare a let
variable will throw an error:
π Example:
let jar = "Tomatos";
let jar = "Spices"; // Error: Identifier 'jar' has already been declared
Unlike var
, variables declared with let
are not hoisted in the same way:
π Example:
console.log(jar); // ReferenceError: Cannot access 'jar' before initialization
let jar = "Tomatos";
3οΈβ£ const (Immutable)
Scope: Block-scoped, like let
Usage: Use const
for values that should not change after being initialized.
const
is perfect for declaring constants or variables that shouldnβt be reassigned.
π Example:
const packet = "Milk";
packet = "Juice"; // Error: Assignment to constant variable
You must initialize a const
variable when declaring it:
π Example:
const packet; // Error: Missing initializer in const declaration
Tasks to Explore Variables
Here are some examples to test your understanding of variables:
/* Task 1: */
let age = 4;
console.log(age); // Outputs: 4
/* Uncomment the following to see errors: */
/* const age = 10; // SyntaxError: Identifier 'age' has already been declared */
/* Task 2: */
const salary = 2000; // Correct
console.log(salary);
/* Task 3: */
/* const salary; // SyntaxError: Missing initializer in const declaration */
/* Task 4: */
/* const salary = 0;
console.log(salary);
salary = 10; // TypeError: Assignment to constant variable */
Conclusion
Mastering variables is the first step toward writing clean and efficient JavaScript code.
- Use
const
whenever possible for stability. - Use
let
only when the value needs to change. - Avoid
var
to prevent hard-to-debug issues.
Happy coding! π
Top comments (0)