JS type conversion

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:

car.owner = "Bill";

// Add element:

cars.push("Porsche");
Example 1: Constant object
// Create object:
const car = {type:"Porsche", model:"911", color:"white"};
// Change property:
// Add property:

car.owner = "Bill";

Try it yourself

Syntax name const value=

;

; Description
name

Required. The name of the variable.

The variable name must follow the following rules:

  • Must start with a letter, $, or _
  • Names are case-sensitive (y and Y are different)
  • Reserved JavaScript keywords cannot be used as names
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