-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
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;
var
Introduced in older versions of JavaScript
Function-scoped
Can be redeclared and reassigned
var name = "Alice";
let
Introduced in ES6 (modern JS)
Block-scoped
Can be reassigned but not redeclared in the same scope
let age = 25;
const
Block-scoped
Cannot be reassigned or redeclared
Must be initialized at declaration
const country = "India";
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
You can declare a variable and assign a value later.
let score;
score = 100;
Only var
and let
allow reassignment:
let a = 5;
a = 10; // valid
const b = 15;
b = 20; // ❌ Error - reassignment not allowed
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?
Q1: Which keyword is used to declare a variable in JavaScript introduced in ES6?
Q2: Which of the following variable declarations is NOT allowed in JavaScript?
Q3: Which statement about const variables is correct?
Q4: Which keyword allows you to redeclare a variable in the same scope?
Q5: What is the default value of a declared but uninitialized variable?
Q6: Which of the following variable names is valid in JavaScript?
Q7: What will happen if you try to reassign a value to a variable declared with const?
Q8: Which statement is true about variable scope?
Q9: Which keyword is used to declare a constant value in JavaScript?
Q10: Which of the following best describes the purpose of variables in JavaScript?