Full support for the const keyword started from the following browser versions:
In JavaScript, const
variables, including arrays, must be assigned a value at the time of declaration. Declaring an array with const
without initializing it will cause a syntax error.
Example
This approach will not function.
const cars; cars = [“Saab”, “Volvo”, “BMW”]; |
Arrays declared with var
can be initialized later and used before their declaration due to hoisting.
Example
This is acceptable.
cars = [“Saab”, “Volvo”, “BMW”]; var cars; |