JavaScript const Statement Example


const : declares a block-scoped variable with a constant value

constant : a situation or state of affairs that does not change

The scope of the constant variable is restricted to the block in which it is declared.

The value of the constant variable cannot be changed.

A variable declared using const must be initialized when it is declared.

JavaScript const Example

var num = 10;

console.log("num = " + num);
// At this point, num = 10

{
    const num = 5;
    console.log("num = " + num);
    // At this point, num = 5
}

console.log("num = " + num);
// At this point, num = 10

Output from The Program

num = 10
num = 5
num = 10

const statement (JavaScript)

張貼留言

0 留言