JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Variables


📘 JavaScript Variables – Storing and Managing Data

Variables in JavaScript are containers used to store data values, like numbers, strings, or objects.

JavaScript allows you to declare variables in three ways:

var x = 10;
let y = 20;
const z = 30;

🔹 Declaring Variables

1️⃣ var
  • Introduced in older versions of JavaScript

  • Function-scoped

  • Can be redeclared and reassigned

var name = "Alice";

2️⃣ let
  • Introduced in ES6 (modern JS)

  • Block-scoped

  • Can be reassigned but not redeclared in the same scope

let age = 25;

3️⃣ const
  • Block-scoped

  • Cannot be reassigned or redeclared

  • Must be initialized at declaration

const country = "India";

🔹 Rules for Naming Variables

  • Names must begin with a letter, underscore _, or dollar sign $

  • Names cannot start with a number

  • Names are case-sensitive

  • Avoid using reserved keywords (like let, var, if, etc.)

let firstName = "John"; // valid
let 1stName = "John";   // ❌ invalid

🔹 Assigning Values Later

You can declare a variable and assign a value later.

let score;
score = 100;

🔹 Reassigning Variables

Only var and let allow reassignment:

let a = 5;
a = 10; // valid

const b = 15;
b = 20; // ❌ Error - reassignment not allowed

 


Practice Questions

Q1. How do you declare a variable named username using let and assign it the value "admin"?

Q2. How do you declare a variable with const that stores the value 3.14 in a variable named PI?

Q3. How do you declare a var variable named city without assigning a value, and then assign "Mumbai" to it later?

Q4. How do you demonstrate that a let variable cannot be redeclared in the same scope?

Q5. How can you reassign a value to a variable count declared with let?

Q6. How do you write a JavaScript code that shows the difference between const and let in terms of reassignment?

Q7. How do you declare a variable with a valid name using an underscore (e.g., _userId) and assign the value 101?

Q8. How do you show that var allows redeclaration by declaring the same variable twice?

Q9. What happens when you try to use a reserved keyword like if as a variable name in JavaScript? Demonstrate with an example.

Q10. How do you declare three variables a, b, and c in a single line using let and assign them values 1, 2, and 3 respectively?


JS Variables Quiz

Q1: Which keyword is used to declare a variable in JavaScript introduced in ES6?

A. var
B. define
C. let
D. dim

Q2: Which of the following variable declarations is NOT allowed in JavaScript?

A. let $name = "John";
B. let 1value = 10;
C. let _id = 5;
D. let fullName = "Alice";

Q3: Which statement about const variables is correct?

A. They can be updated and reassigned
B. They cannot be reassigned after declaration
C. They can be declared without initialization
D. They are function-scoped

Q4: Which keyword allows you to redeclare a variable in the same scope?

A. let
B. const
C. var
D. None

Q5: What is the default value of a declared but uninitialized variable?

A. null
B. 0
C. undefined
D. false

Q6: Which of the following variable names is valid in JavaScript?

A. 123name
B. user-name
C. _total
D. function

Q7: What will happen if you try to reassign a value to a variable declared with const?

A. It works normally
B. A warning is shown
C. A syntax error occurs
D. It ignores the reassignment

Q8: Which statement is true about variable scope?

A. var is block-scoped
B. let and const are function-scoped
C. let is block-scoped
D. All are block-scoped

Q9: Which keyword is used to declare a constant value in JavaScript?

A. const
B. static
C. final
D. constant

Q10: Which of the following best describes the purpose of variables in JavaScript?

A. To style HTML pages
B. To store data values
C. To write comments
D. To create animations

Go Back Top