Master Variable JavaScript Examples

Sarah Jul 13, 2026

Variable data types play a crucial role in JavaScript, enabling dynamic and flexible programming. One of the most significant aspects of these variables is their mutability - the ability to change values after being declared. Let's explore some examples of JavaScript variables, their declarations, and how they can be manipulated.

Master JavaScript Variables: A Beginner’s Guide πŸš€
Master JavaScript Variables: A Beginner’s Guide πŸš€

Before diving into variable examples, it's essential to understand the concept of data types. JavaScript is a dynamically typed language, meaning you don't need to specify the type of a variable when declaring it. Instead, the JavaScript engine infers the data type based on the value assigned to the variable. Now, let's delve into variable declarations and examples.

What is Variable and Function Hoisting in JavaScript? Example Tutorial
What is Variable and Function Hoisting in JavaScript? Example Tutorial

Variable Declaration and Initialization

In JavaScript, you can declare variables using the `var`, `let`, or `const` keywords. While `var` is function-scoped, `let` and `const` are block-scoped. Moreover, `const` declarations are immutable and cannot be reassigned. Let's look at some examples:

What are Variables in JavaScript?
What are Variables in JavaScript?

Using `var` for Function-scoped Variables

`var` is function-scoped, meaning variables declared with `var` inside a function are only accessible within that function.

an image of a poster with some type of text on it, including numbers and symbols
an image of a poster with some type of text on it, including numbers and symbols

```javascript function exampleVar() { var x = 10; if (true) { var x = 20; console.log(x); // 20 } console.log(x); // 20 } exampleVar(); ```

Using `let` and `const` for Block-scoped Variables

`let` and `const` are block-scoped, meaning they are only accessible within the block (e.g., `if`, `switch`, `for`, or `while` statements) where they are declared.

a diagram showing the different types of data
a diagram showing the different types of data

```javascript function exampleLetConst() { let i = 10; if (true) { const i = 20; console.log(i); // 20 } console.log(i); // 10 } exampleLetConst(); ```

Variable Mutation

In JavaScript, variables are mutable by default, allowing you to change their values. The type of a mutable variable can also change. Let's explore some examples of mutable variables:

JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)

Mutation of Primitive Data Types

Primitive data types like `Number`, `String`, and `Boolean` can be mutated by reassigning their values. However, the actual memory location doesn't change - only the value stored in that location does.

The JavaScript Cheat Sheet Every Beginner Wishes They Had
The JavaScript Cheat Sheet Every Beginner Wishes They Had
Free JavaScript Book
Free JavaScript Book
Declaring a variable in JavaScript using the let keyword
Declaring a variable in JavaScript using the let keyword
JavaScript Basics for Beginners | Learn JS Step by Step
JavaScript Basics for Beginners | Learn JS Step by Step
How to Declare Variables in JavaScript
How to Declare Variables in JavaScript
JS variables
JS variables
JavaScript Basics You MUST Know
JavaScript Basics You MUST Know
Declear Variables
Declear Variables
JavaScript Arrow Functions: Write Shorter & Cleaner Code πŸš€
JavaScript Arrow Functions: Write Shorter & Cleaner Code πŸš€
JavaScript Full Notes Every Beginner Must Save πŸ’‘
JavaScript Full Notes Every Beginner Must Save πŸ’‘
JavaScript Variables | Declaration, Example
JavaScript Variables | Declaration, Example
JavaScript Cheat Sheet You Must Save | Beginner Friendly Guide
JavaScript Cheat Sheet You Must Save | Beginner Friendly Guide
JavaScript study guide for fast learning
JavaScript study guide for fast learning
Functions in Javascript
Functions in Javascript
JavaScript Variable Types Cheat Sheet
JavaScript Variable Types Cheat Sheet
πŸš€ JavaScript Cheat Sheet for Beginners & Intermediate Developers | Quick Reference Guide
πŸš€ JavaScript Cheat Sheet for Beginners & Intermediate Developers | Quick Reference Guide
JavaScript Return Values: Get the Most Out of Your Functions πŸš€
JavaScript Return Values: Get the Most Out of Your Functions πŸš€
Variables in JavaScript Explained Simply
Variables in JavaScript Explained Simply
JavaScript Variables Explained: Box Analogy for Beginners
JavaScript Variables Explained: Box Analogy for Beginners
10 JavaScript Concepts Every Beginner Must Know (Save This Cheat Sheet!)
10 JavaScript Concepts Every Beginner Must Know (Save This Cheat Sheet!)

```javascript let num = 10; num = 20; console.log(num); // 20 let str = 'hello'; str = 'world'; console.log(str); // world ```

Object Mutation

Objects in JavaScript are also mutable. You can change their properties directly, or use methods like `Object.assign()` or spread syntax to create copies with modified properties.

```javascript let obj = { a: 1, b: 2 }; obj.a = 10; console.log(obj); // {a: 10, b: 2} let newObj = { ...obj, c: 3 }; console.log(newObj); // {a: 10, b: 2, c: 3} ```

In the world of software development, variables are fundamental building blocks. Understanding how variables behave in JavaScript equips you with the knowledge to build adaptable, dynamic applications. With the ability to declare and manipulate variables, the possibilities are boundless - so start exploring and coding!