Which keyword is used to declare variables in JavaScript that cannot be reassigned?
Question
Which keyword is used to declare variables in JavaScript that cannot be reassigned?
Solution
In JavaScript, the keyword used to declare variables that cannot be reassigned is const
. When you declare a variable using const
, it is a constant reference and its value cannot be changed through reassignment. However, it's important to note that if the value is an object or an array, the contents of that object or array can still be modified. Here's an example:
const myNumber = 5;
// myNumber = 10; // This will throw an error because reassignment is not allowed.
const myArray = [1, 2, 3];
myArray.push(4); // This is allowed, as we are modifying the contents of the array.
Overall, const
ensures that the variable's identifier cannot be reassigned, thus maintaining the intended value throughout the code.
Similar Questions
Which of the following is not a valid way to declare a JavaScript variable?Optionsvar x;const x;variable x;let x;
Which of the following is the correct way to declare a variable in JavaScript?variable name = "John";var name = "John";let name == "John";name: "John";
What is the correct way to declare a variable in JavaScript?Points:1All of the abovevar myVariable = 10;I don't knowlet myVariable = 10;const myVariable = 10;
What is the correct syntax to declare a variable in JavaScript?var myVariable;variable myVariable;Both a and clet myVariable;
What will the value of Var be in the following code snippet?var Var='var';var Var;
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.