-
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
const
– Declaring Constants in JavaScriptThe const
keyword in JavaScript is used to declare block-scoped, read-only variables. Once a variable is declared with const
, its value cannot be reassigned.
const pi = 3.14159;
const
:🟡 Must be initialized at declaration
🟡 Cannot be reassigned
🟡 Is block-scoped like let
🟡 Does not allow redeclaration in the same scope
const name = "John";
const age; // ❌ Error: Missing initializer
const age = 30;
age = 35; // ❌ Error: Assignment to constant variable
const
With Objects and Arraysconst
does not make objects/arrays immutable, but it prevents reassignment of the variable itself.
const person = { name: "Alice", age: 25 };
person.age = 30; // ✅ Allowed
person = { name: "Bob" }; // ❌ Error
const nums = [1, 2, 3];
nums.push(4); // ✅ Allowed
nums = [4, 5]; // ❌ Error
let
and const
Feature | let |
const |
---|---|---|
Reassignment | ✅ Allowed | ❌ Not Allowed |
Initialization | Optional | ✅ Required |
Scope | Block-scoped | Block-scoped |
Redeclaration | ❌ Not allowed | ❌ Not allowed |
Q1. How do you declare a constant variable named country
with the value "India"
using const
?
Q2. How do you demonstrate that reassignment of a const
variable causes an error?
Q3. How do you declare a constant object car
and modify one of its properties (e.g., change color)?
Q4. How do you declare a constant array fruits
with three elements and add a fourth element to it?
Q5. How do you show that redeclaring a const
variable in the same scope causes an error?
Q6. How do you demonstrate that const
is block-scoped using an if
block?
Q7. How do you create a constant object user
and log one of its properties (like user.name
)?
Q8. How do you explain the difference between modifying an object vs. reassigning a const
object? Show with code.
Q9. What happens if you declare a const
variable without assigning a value? Show with code.
Q10. How do you write a const
variable named MAX_SCORE
with a numeric value and use it in a condition?
Q1: Which keyword is used to declare a variable that cannot be reassigned?
Q2: What will happen if you try to reassign a const variable?
Q3: Which of the following is TRUE about const variables?
Q4: What is allowed with a const object?
Q5: Which of the following will cause an error?
Q6: What will this code do? const city; city = "Delhi";
Q7: What happens if you modify the contents of an array declared with const?
Q8: Which is a valid const declaration?
Q9: Can const be used to declare block-scoped variables?
Q10: What happens when you try to redeclare a const variable in the same block?