JavaScript Variables

Variables are fundamental building blocks in JavaScript. They act as containers for storing data values. Think of them as named boxes where you can put information that your program needs to use or change.

Declaring Variables

In JavaScript, you declare variables using the var, let, or const keywords. Each has its own scope and behavior.

Using var

var is the oldest way to declare variables in JavaScript. Variables declared with var are function-scoped or globally scoped, meaning they are accessible throughout the function they are declared in, or globally if declared outside of any function.


var name = "Alice";
var age = 30;
var isStudent = false;

console.log(name); // Output: Alice
console.log(age);  // Output: 30
            

Using let

let was introduced in ECMAScript 2015 (ES6) and allows you to declare variables that are block-scoped. This means they are only accessible within the block (e.g., within an if statement or a for loop) where they are declared.


let city = "New York";
city = "Los Angeles"; // You can reassign values to let variables

if (true) {
    let message = "Hello!";
    console.log(message); // Output: Hello!
}
// console.log(message); // This would cause an error: message is not defined
            

Using const

const also allows for block-scoped declarations, similar to let. However, the value assigned to a const variable cannot be reassigned after it has been initialized. It's used for values that should remain constant throughout the program's execution.


const PI = 3.14159;
// PI = 3.14; // This would cause an error: Assignment to constant variable.

const person = { name: "Bob" };
person.name = "Charlie"; // This is allowed, as you're modifying the object's property, not reassigning the variable itself.
console.log(person.name); // Output: Charlie
            

Best Practices:

It's generally recommended to use let for variables that might change and const for variables that should not be reassigned. Avoid using var in modern JavaScript development to prevent potential scope-related issues.

Variable Naming Rules

Assigning Values

You can assign a value to a variable when you declare it, or at a later point using the assignment operator (=).


// Declaration and assignment in one step
let counter = 0;

// Declaration and then assignment
let score;
score = 100;
            

Data Types Stored in Variables

Variables can hold various types of data, including:

JavaScript is dynamically typed, meaning you don't need to explicitly declare the type of data a variable will hold. The type is inferred at runtime.

Checking Variable Types

The typeof operator can be used to determine the data type of a variable.


let userName = "Bob";
let userAge = 25;
let isActive = true;

console.log(typeof userName); // Output: string
console.log(typeof userAge);  // Output: number
console.log(typeof isActive); // Output: boolean
            

Understanding variables is crucial for writing any JavaScript code. They are the foundation upon which more complex logic is built.