Constants are block-scoped, much like variables declared using the nameN The constant's name, which can be any legal identifier. valueN The constant's value. This can be any legal expression, including a function expression. The destructuring assignment syntax can also be used to declare variables.
Description This declaration creates a constant whose scope can be either global or local to the block in which it is declared. Global constants do not become properties of the
An initializer for a constant is required. You must specify its value in the same declaration. (This makes sense, given that it can't be changed later.) The All the considerations about the temporal dead zone apply to both
A constant cannot share its name with a function or a variable in the same scope. Unlike
ExamplesBasic const usageConstants can be declared with uppercase or lowercase, but a common convention is to use all-uppercase letters.
Block scopingIt's important to note the nature of block scoping.
const needs to be initialized
const in objects and arrays
However, object keys are not protected, so the following statement is executed without problem.
You would need to use
The same applies to arrays. Assigning a new array to the variable throws an error "Assignment to constant variable".
Still, it's possible to push items into the array and thus mutate it.
Specifications
Browser compatibilityBCD tables only load in the browser See alsoIs a name that references a value that Cannot be changed while the program runs?A constant is a data item whose value cannot change during the program's execution. Thus, as its name implies – the value is constant. A variable is a data item whose value can change during the program's execution.
Which of the following functions returns the largest integer that is less than or equal to its argument?Description. The floor() function calculates the largest integer that is less than or equal to x.
When a function is called by its name during the execution of a program then it is?When a function is called by its name during the execution of a program, then it is. executed.
What is a function header quizlet?function header. shows the function name, return type and parameter list; int main() void functions.
|