JS Variables

1. What is a Variable?

Variables are containers used to store data such as text, numbers, or boolean values. 
They let you set, update, and access data whenever needed. 

Think of a variable like a named box where you can put information and retrieve it later. 

JS Variables

2. Variable Naming Rules

When naming JavaScript variables, follow these rules: 

  •  Must start with a letter, underscore (_) or dollar sign ($) 
  •  Can contain letters, numbers, and underscores 
  •  Cannot start with a number 
  •  Cannot contain spaces 
  •  Cannot use JavaScript keywords or reserved words 
  •  Use camelCase convention 
    (Example: firstName, userAge) 

3. Declaring Variables

There are three ways to declare variables: 

1. Using var (old way)

var name = “John loyds”; 

var age = 24; 

var isGood = true; 

 

// Separate declaration and assignment 

var username, age; 

userName = “Aayesha Khan”; 

age=10; 

  • Function scoped : Variable declared inside a function is accessible only within that function and not outside it. 
  • Can be re-declared and reassigned 
  • Hoisted to the top of their scope means accessible before declaration (with undefined value) 

2. Using let (ES6)

let characterName = “Harry Potter”; 

let characterAge = 11; 

let isCharacterStudent = true; 

  • Block scoped { } 
  • Can be reassigned 
  • Cannot be re-declared in the same scope 
  • Not hoisted (inaccessible before declaration) 

3.Using const (ES6)

const PI = 3.14; 

console.log(PI); // 3.14 

 

PI = 10; // Error: Cannot reassign a constant 

    • Block scoped { } 
    • Cannot be reassigned and redeclared 
    • JavaScript const variables must be assigned a value when they are declared 

4. Hoisting Behavior

Hoisting moves variable declarations to the top of their scope. In simple words, you can use a variable or function before declaring it — but only with var (and functions). 
Only var is hoisted with undefined, while let and const are not accessible before they are declared. 

console.log(a); // undefined 

var a = 5; 

 

console.log(b); // ReferenceError 

let b = 10; 

 

console.log(c); // ReferenceError 

const c = 15; 

5. Scope of Variables

Example: 

if (true) { 

  var a = 1; 

  let b = 2; 

  const c = 3; 

} 

console.log(a); // 1 

console.log(b); //  ReferenceError 

console.log(c); //  ReferenceError 

6. Re-declaration vs Re-assignment

var x = 1; 

var x = 2; //  re-declare allowed 

 

let y = 1; 

// let y = 2;  re-declare not allowed 

y = 2; //  reassignment allowed 

 

const z = 1; 

// z = 2;  reassignment not allowed 

7. Re-declaration vs Re-assignment

  •  Prefer const by default 
  •  Use let only when reassignment is needed 
  •  Avoid var in modern code 
  •  Use meaningful variable names 
  •  Use camelCase naming style 

Course Video in Hindi

Course Video in English

Frequently Asked Questions

Still have a question?

Let's talk

Variables are named containers that store data values in JavaScript, allowing developers to manipulate and use data within their programs.

You can declare variables using var, let, or const. Example:

let x = 5; 

const y = 10; 

var z = 15;

var is function-scoped and can be redeclared.

  • let is block-scoped and cannot be redeclared in the same scope.
  • const is block-scoped and its value cannot be reassigned.

Yes, you can declare a variable without a value. It will have the value undefined by default. Example:

let myVariable; 

console.log(myVariable); // undefined

  • Variable names must begin with a letter, _, or $.
  • They cannot contain spaces or special characters.
  • JavaScript is case-sensitive (myVar and myvar are different).

No, reserved keywords like if, else, and return cannot be used as variable names.

You can reassign values to variables declared with var or let, but not with const. Example:

let num = 10; 

num = 20; // Allowed

  • Global variables: Declared outside functions and accessible anywhere in the code.
  • Local variables: Declared within a function and only accessible within that function.

Using an undeclared variable automatically creates it as a global variable (in non-strict mode), which is not recommended.

Hoisting is JavaScript’s default behavior of moving declarations to the top of their scope during execution. However, var variables are initialized as undefined, while let and const are not initialized.

In JavaScript, you can declare variables using var, let, or const. For example:

javascript

Copy code

let name = “John”; 

const age = 25; 

var isActive = true;

You can learn more about declaring variables in our Free JavaScript variables tutorial for beginners.

The main differences between var, let, and const in JavaScript are related to scope, hoisting, and reassignment.

  • var is function-scoped and can be redeclared.
  • let is block-scoped and can be reassigned but not redeclared in the same scope.
  • const is block-scoped and cannot be reassigned or redeclared.
    For more details, check our Differences between var, let, and const in JavaScript tutorial.

We offer a Free JavaScript variables tutorial for beginners, where you will learn everything you need to know about declaring and working with variables in JavaScript, including practical examples.

Yes, our website provides a JavaScript variables free course video, which walks you through the concepts of variables in JavaScript, such as how to declare them and the differences between var, let, and const.