Q1. What is a Variable in JavaScript?
A variable is a container used to store data values. It allows developers to store, update, and reuse data in a program.
Example
Output
Here, name is a variable that stores the value "John".
Example
let name = "John";console.log(name);Output
JohnHere, name is a variable that stores the value "John".
Q2. How Do You Declare Variables in JavaScript?
JavaScript provides three keywords to declare variables:
Example
Each keyword has different behavior and usage rules.
- var
- let
- const
var age = 20;let city = "London";const country = "UK";Each keyword has different behavior and usage rules.
Q3. What is Variable Scope?
Scope defines where a variable can be accessed in the program.
Types of Scope
1. Global Scope
Variable cna be accessed anywhere.
2. Block Scope
Variable can only be accessed inside a block { }.
Types of Scope
1. Global Scope
Variable cna be accessed anywhere.
let name = "Alex";2. Block Scope
Variable can only be accessed inside a block { }.
{ let age = 25;}Q4. What is Variable Initialization?
Initialization means assigning a value to a variable when it is declared.
Example
Here:
Example
let score = 100;Here:
- score → Variable name
- 100 → Assigned value
