// Create an array:
const cars = ["Saab", "Volvo", "BMW"];
// Change an element:
cars[0] = "Toyota";
// Add an element:
cars.push("Audi");
Try it Yourself »
// Create an object:
const car = {type:"Fiat", model:"500", color:"white"};
// Change a property:
car.color = "red";
// Add a property:
car.owner = "Johnson";
Try it Yourself »
The const
statement declares a variable.
Variables are containers for storing information.
Creating a variable in JavaScript is called "declaring" a variable:
const name = "Volvo";
A const variable must be assigned when it is declared.
JavaScript Reference: JavaScript var
JavaScript Reference: JavaScript let
JavaScript Tutorial: JavaScript Variables
JavaScript Tutorial: JavaScript const
JavaScript Tutorial: JavaScript let
JavaScript Tutorial: JavaScript Scope
const
name =
value;
Parameter | Description |
name | Required. The name of the variable. Variable names must follow these rules: Must begin with a letter, or $, or _ Names are case sensitive (y and Y are different) Reserved JavaScript words cannot be used as names |
value | Required. A value to be assigned to the variable. |
As a general rule, always declare a variable with const
unless you know that the value will change.
Use const
when you declare:
const
is supported in all modern browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 11 |
const
is an ECMAScript6 (ES6 - JavaScript 2015) feature.
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!