-
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
let
– Block Scoped Variable DeclarationThe let
keyword was introduced in ES6 (2015) and is used to declare variables that are block-scoped, meaning they are only accessible within the block {}
in which they are defined.
let x = 5;
let name = "John";
let
?Block scope (unlike var
which is function-scoped)
Prevents accidental redeclarations in the same scope
Safer and more predictable for loops, conditions, and modern JS practices
{
let x = 10;
console.log(x); // ✅ 10
}
console.log(x); // ❌ ReferenceError: x is not defined
let
in the Same Scopelet name = "Alice";
let name = "Bob"; // ❌ Error: Identifier 'name' has already been declared
let
Variableslet city = "Delhi";
city = "Mumbai"; // ✅ This is allowed
var
and let
Feature | var |
let |
---|---|---|
Scope | Function-scoped | Block-scoped |
Redeclaration | Allowed | Not allowed |
Hoisting | Hoisted (value: undefined ) |
Hoisted (but not initialized) |
Reassignment | Allowed | Allowed |
Q1. How do you declare a variable named age
using the let
keyword and assign it the value 30
?
Q2. How do you update a variable city
declared with let
to change its value from "Delhi"
to "Mumbai"
?
Q3. How do you demonstrate block scope by declaring a let
variable inside a block and trying to access it outside the block?
Q4. How do you show that a variable declared with let
cannot be redeclared in the same scope?
Q5. How do you use a let
variable in a for
loop to iterate from 1
to 5
and print each number?
Q6. How do you write a JavaScript code that declares the same variable name with let
inside and outside a block to show they are treated separately?
Q7. How do you declare a variable x
with let
, assign it 10
, then reassign it to 20
and log the result?
Q8. How can you demonstrate that let
declarations are hoisted but not initialized?
Q9. How do you write a function with a let
variable that does not affect the global variable with the same name?
Q10. How do you write a condition using if
block where a let
variable is declared inside and used only within that block?
Q1: What does the let keyword do in JavaScript?
Q2: Which of the following is true about let declarations?
Q3: What happens if you try to access a let variable before it is declared?
Q4: Which keyword prevents redeclaring the same variable in the same scope?
Q5: Which variable declaration is block-scoped?
Q6: What happens if you redeclare a let variable in the same block?
Q7: Can you reassign a value to a let variable?
Q8: Which of the following best describes the scoping of let?
Q9: Which of the following is NOT allowed when using let?