Variables
Variables in JavaScript are fundamental constructs used to store and manipulate data. They act as containers for data values and can hold various types of data such as numbers, strings, objects, arrays, and more. Understanding how to declare, initialize, and use variables is crucial for effective JavaScript programming.
Declaring Variables
In JavaScript, variables can be declared using three main keywords: var
, let
, and const
. Each has its own scope and characteristics.
var
var
is the traditional way to declare variables. It is function-scoped, meaning the variable is accessible within the function it is declared in, or globally if declared outside any function. Variables declared withvar
can be re-declared and updated.
Example:
var name = "Alice"; console.log(name); // Output: Alice var name = "Bob"; // Re-declaration is allowed console.log(name); // Output: Bob
let
let
is a more modern way to declare variables. It is block-scoped, meaning the variable is only accessible within the block (enclosed by{}
) it is declared in. Variables declared withlet
can be updated but not re-declared within the same scope.
Example:
let age = 25; console.log(age); // Output: 25 age = 30; // Updating the variable console.log(age); // Output: 30 // let age = 35; // Error: Identifier 'age' has already been declared
const
const
is used to declare constants—variables that cannot be reassigned after their initial value is set. Likelet
,const
is block-scoped. It is used for values that should remain constant throughout the code.
Example:
const PI = 3.14159; console.log(PI); // Output: 3.14159 // PI = 3.14; // Error: Assignment to constant variable
Variable Types
JavaScript variables can hold different types of data, including:
Primitive Data Types
- Number: Represents numeric values (both integer and floating-point).
let num = 42;
- String: Represents sequences of characters, enclosed in single or double quotes.
let greeting = "Hello, World!";
- Boolean: Represents
true
orfalse
values.let isActive = true;
- Undefined: Represents a variable that has been declared but not yet assigned a value.
let result; console.log(result); // Output: undefined
- Null: Represents an intentional absence of value.
let data = null;
- Symbol: Represents a unique and immutable value often used as object property keys.
let uniqueId = Symbol('id');
- Number: Represents numeric values (both integer and floating-point).
Complex Data Types
- Object: Represents a collection of key-value pairs. Useful for grouping related data.
let person = { name: "Alice", age: 30 };
- Array: Represents a list-like collection of values.
let fruits = ["Apple", "Banana", "Cherry"];
- Function: Represents a block of code that can be executed when called.
function greet() { return "Hello!"; }
- Object: Represents a collection of key-value pairs. Useful for grouping related data.
Variable Scope
Scope refers to the accessibility of variables in different parts of the code.
Global Scope
- Variables declared outside any function or block are globally scoped and accessible from anywhere in the code.
Function Scope
- Variables declared within a function using
var
are function-scoped and can only be accessed within that function.
- Variables declared within a function using
Block Scope
- Variables declared within a block (using
let
orconst
) are block-scoped and can only be accessed within that block.
Example:
function example() { var x = 10; // Function-scoped let y = 20; // Block-scoped if (true) { var z = 30; // Function-scoped let w = 40; // Block-scoped console.log(x); // Output: 10 console.log(y); // Output: 20 console.log(z); // Output: 30 console.log(w); // Output: 40 } console.log(x); // Output: 10 console.log(y); // Output: 20 console.log(z); // Output: 30 // console.log(w); // Error: w is not defined }
- Variables declared within a block (using
Variable Hoisting
In JavaScript, variable declarations using var
are "hoisted" to the top of their scope, meaning they are processed before any code is executed. However, their initial values are not hoisted, only the declaration itself.
Example:
console.log(name); // Output: undefined
var name = "Alice";
console.log(name); // Output: Alice
Variables declared with let
and const
are not hoisted in the same way and will throw an error if accessed before their declaration.