JavaScript

coding learning websites codepractice

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?


JavaScript

online coding class codepractice

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

Go Back Top