Q
How can you ensure variable immutability within a block?

Answer & Solution

Answer: Option C
Solution:
Variables declared with const are immutable within their block and cannot be reassigned.
Related Questions on Average

What happens when you declare a constant with const inside a block and try to reassign it?

A). It throws an error

B). It allows reassignment

C). It assigns the value globally

D). It assigns a default value

What is block scope in JavaScript?

A). Variables scoped globally

B). Variables scoped within a function

C). Variables scoped to a block

D). Variables scoped to an object

What is the output of console.log(blockVar); outside a block if let blockVar = 'Block'; is declared inside a block?

A). Block

B). undefined

C). Error

D). null

What is lexical scoping in JavaScript?

A). Variables scoped to the entire codebase

B). Variables scoped to their functions

C). Variables scoped based on their position in the code

D). Variables scoped based on their names

What is the purpose of block scope in JavaScript?

A). To limit the visibility and accessibility of variables

B). To make variables global

C). To create constants

D). To execute functions

How can you access a variable declared in an inner block from an outer block?

A). Using the var keyword

B). Using the let keyword

C). Using the const keyword

D). Using the this keyword

How does block scope affect the visibility of variables in nested blocks?

A). Variables are only visible within their immediate block

B). Variables are visible within all nested blocks

C). Variables are only visible within functions

D). Variables are globally visible

What is the benefit of block scope in loops?

A). It allows variables to be reassigned within each iteration

B). It prevents variable hoisting

C). It restricts variables to the loop block

D). It makes variables globally accessible

What is the main difference between var and let in terms of scope?

A). var is block-scoped, let is function-scoped

B). var is function-scoped, let is block-scoped

C). Both var and let are block-scoped

D). Both var and let are function-scoped

Which keyword introduces block-scoped variables in JavaScript?

A). var

B). let

C). const

D). function