Mapagam
  • JavaScript 
  • Web APIs 
  • TypeScript 
  • React 

What Are Variables in JavaScript? A Simple Explanation

Posted on March 31, 2025 • 6 min read • 1,090 words
Share via
Mapagam
Link copied to clipboard

Learn what variables are in JavaScript, how to declare them, and understand concepts like scope, hoisting, and data types in this simple guide.

On this page
1. Introduction to Variables in JavaScript 1.1 What Is a Variable? 1.2 Why Are Variables Important? 2. Declaring Variables in JavaScript 2.1 The var Keyword 2.2 The let Keyword 2.3 The const Keyword 2.4 Which One to Use? 3. Data Types Stored in Variables 3.1 Primitive Data Types 3.2 Complex Data Types 4. Understanding Variable Scope 4.1 What Is Scope? 4.2 Function Scope vs. Block Scope 5. Hoisting in JavaScript 5.1 What Is Hoisting? 5.2 Hoisting with let and const 6. Conclusion

In the world of programming, variables are an essential concept, and JavaScript is no exception. As one of the most widely used programming languages for building websites and web applications, understanding how variables work in JavaScript is crucial.

1. Introduction to Variables in JavaScript

Variables are fundamental to programming. In essence, a variable is a container used to store data that can be accessed and manipulated later in the code. JavaScript, like most programming languages, uses variables to hold different types of data, such as numbers, strings, objects, and more.

1.1 What Is a Variable?

A variable is a symbolic name for a value in memory. Instead of directly working with raw values, variables give us a way to store, retrieve, and modify data dynamically throughout the execution of a program.

For example:

let age = 25;

In this example, age is the variable, and it stores the value 25. You can later change the value of age or use it in calculations.

1.2 Why Are Variables Important?

Without variables, we would have to manually manage values and would be unable to store and update data dynamically. Variables make your code more efficient and flexible by enabling you to store data that can be reused, modified, or passed around.

2. Declaring Variables in JavaScript

In JavaScript, you can declare variables using three main keywords: var, let, and const. Each one has different characteristics and uses, which we’ll explain in detail.

2.1 The var Keyword

The var keyword was traditionally used in JavaScript to declare variables. It’s function-scoped, meaning that the variable is available within the function where it’s declared, or globally if declared outside any function.

However, var has some issues, such as being prone to unexpected behavior due to its function-scoping and hoisting. Therefore, its use is now generally discouraged in favor of let and const.

Example using var:

var name = "John";
console.log(name); // Outputs: John

2.2 The let Keyword

Introduced in ECMAScript 6 (ES6), the let keyword provides block-scoping. This means that a variable declared with let is only accessible within the block (e.g., within a function, loop, or conditional statement) where it’s defined.

Example using let:

let city = "New York";
if (true) {
  let city = "Los Angeles"; // A new variable with the same name but scoped within the block
  console.log(city); // Outputs: Los Angeles
}
console.log(city); // Outputs: New York

In this example, you can see that the variable city is scoped to the if block, and does not affect the outer city variable.

2.3 The const Keyword

The const keyword is used to declare variables whose values cannot be reassigned once initialized. Like let, const is also block-scoped, but the difference is that you cannot change the value of a const variable after it’s been assigned.

Example using const:

const country = "Canada";
// country = "Mexico"; // This will throw an error: Assignment to constant variable.
console.log(country); // Outputs: Canada

However, it’s important to note that objects and arrays declared with const can still have their contents modified, but the variable itself cannot be reassigned.

2.4 Which One to Use?

  • var: Avoid using it in modern JavaScript.
  • let: Use when the value of the variable needs to change later in the program.
  • const: Use when the variable should not be reassigned after initialization.

3. Data Types Stored in Variables

Variables in JavaScript can hold different types of data. JavaScript has several primitive data types and complex data types that can be stored in variables.

3.1 Primitive Data Types

Primitive data types include:

  • String: Represents text data, enclosed in single, double, or backticks (template literals).

    let greeting = "Hello, World!";
  • Number: Represents numeric values (both integers and floating-point numbers).

    let price = 19.99;
    let quantity = 5;
  • Boolean: Represents a logical value, either true or false.

    let isActive = true;
  • Undefined: A variable that has been declared but not assigned a value.

    let result;
    console.log(result); // Outputs: undefined
    
  • Null: Represents the intentional absence of any value.

    let selectedItem = null;
  • Symbol: A unique and immutable primitive value.

  • BigInt: Used for working with large integers beyond the safe range of Number.

3.2 Complex Data Types

JavaScript also allows variables to store complex data types such as:

  • Object: A collection of key-value pairs.

    let person = {
      name: "Alice",
      age: 30
    };
  • Array: A list-like collection of ordered elements.

    let colors = ["red", "green", "blue"];

4. Understanding Variable Scope

4.1 What Is Scope?

Scope refers to the region of a program where a variable can be accessed. JavaScript has different levels of scope, primarily global scope and local scope.

  • Global Scope: Variables declared outside any function or block are in the global scope and can be accessed anywhere in the code.
  • Local Scope: Variables declared inside a function or block are in the local scope and can only be accessed within that function or block.

4.2 Function Scope vs. Block Scope

  • Function Scope: A variable declared with var is function-scoped, meaning it can be accessed anywhere within the function.
  • Block Scope: Variables declared with let or const are block-scoped, meaning they’re only accessible within the block they are defined.

Example:

function test() {
  var a = 10;  // function-scoped
  let b = 20;  // block-scoped
  if (true) {
    let b = 30;  // another block-scoped variable
    console.log(b); // Outputs: 30
  }
  console.log(a); // Outputs: 10
  console.log(b); // Outputs: 20
}

5. Hoisting in JavaScript

5.1 What Is Hoisting?

Hoisting is JavaScript’s default behavior of moving variable declarations to the top of their containing scope during the compile phase. However, the values assigned to the variables are not hoisted — only the declarations.

Example:

console.log(x); // Outputs: undefined
var x = 5;
console.log(x); // Outputs: 5

In this example, x is hoisted to the top, but its value 5 is not assigned until later in the code, hence the first console.log(x) outputs undefined.

5.2 Hoisting with let and const

Unlike var, variables declared with let and const are also hoisted, but they are not initialized until the code execution reaches their declaration. Accessing them before initialization results in a ReferenceError.

console.log(y); // ReferenceError: Cannot access 'y' before initialization
let y = 10;

6. Conclusion

Understanding how variables work is a cornerstone of becoming proficient in JavaScript. Variables allow you to store, manipulate, and manage data in your applications, whether you’re building a simple webpage or a complex web application. By understanding how to declare variables, use the right keywords (var, let, const), and grasp concepts like scope and hoisting, you can write cleaner, more efficient code.

JavaScript Variables   JavaScript Scope   Hoisting in JavaScript   JavaScript Data Types   Declaring Variables in JavaScript  
JavaScript Variables   JavaScript Scope   Hoisting in JavaScript   JavaScript Data Types   Declaring Variables in JavaScript  
 JavaScript Data Types Explained: Strings, Numbers, Booleans & More
JavaScript Syntax Explained: Everything You Need to Know 

More Reading!

  1. How JavaScript Variables Actually Work: var vs let vs const
  2. Let vs Var vs Const: Choosing the Right JavaScript Variable
  3. TypeScript Variables: Let, Const, and Var Explained
  4. JavaScript Data Types Explained: Strings, Numbers, Booleans & More
On this page:
1. Introduction to Variables in JavaScript 1.1 What Is a Variable? 1.2 Why Are Variables Important? 2. Declaring Variables in JavaScript 2.1 The var Keyword 2.2 The let Keyword 2.3 The const Keyword 2.4 Which One to Use? 3. Data Types Stored in Variables 3.1 Primitive Data Types 3.2 Complex Data Types 4. Understanding Variable Scope 4.1 What Is Scope? 4.2 Function Scope vs. Block Scope 5. Hoisting in JavaScript 5.1 What Is Hoisting? 5.2 Hoisting with let and const 6. Conclusion
Follow me

I work on everything coding and technology

   
Mapagam
Mapagam is your go-to resource for all things related to frontend development. From the latest frameworks and libraries to tips, tutorials, and best practices, we dive deep into the ever-evolving world of web technologies.
Licensed under Creative Commons (CC BY-NC-SA 4.0).
 
Frontend
JavaScript 
Web Api 
TypeScript 
React 
Social
Linkedin 
Github 
Mapagam
Code copied to clipboard