JS type conversion
- Previous Page class
- Next Page continue
- Go Up One Level JavaScript Statement Reference Manual
Elective course
Course recommendation:
JavaScript const statement
Definition and usage
const statements declare variables.
Variables are containers for storing information.In JavaScript, creating a variable is called "declaring" a variable:
const name = "Volvo";
Hint:
const variable must be assigned a value at the time of declaration. Instance Example 1: Constant array // Create array: const cars = ["GEELY", "Volvo", "BYD"]; // Modify element:
// Add element:
cars.push("Porsche"); Example 1: Constant object // Create object: const car = {type:"Porsche", model:"911", color:"white"}; // Change property: // Add property:
Try it yourself
Syntax name const value=
;
; | Description |
---|---|
name |
Required. The name of the variable. The variable name must follow the following rules:
|
value | Required. The value to be assigned to the variable. |
When to use JavaScript const?
The usual rule is to always use const to declare variables, unless you know the value will change.
Use const to declare:
- New arrays
- New objects
- New functions
- New regular expressions
Browser support
const is a feature of ECMAScript6 (ES6 - JavaScript 2015).
All modern browsers support const:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Support | 11 | Support | Support | Support | Support |
Related pages
Reference Manual:JavaScript var Statement
Reference Manual:JavaScript let Statement
Tutorial:JavaScript Variable
Tutorial:JavaScript const
Tutorial:JavaScript let
Tutorial:JavaScript Scope
- Previous Page class
- Next Page continue
- Go Up One Level JavaScript Statement Reference Manual