- JavaScript Introduction
- JavaScript Where To
- JavaScript Output
- JavaScript Statements
- JavaScript Syntax
- JavaScript Comments
- JavaScript Variables
- JavaScript Let
- JavaScript Const
- JavaScript Operators
- JavaScript Arithmetic
- JavaScript Assignment
- JavaScript Data Types
- JavaScript Functions
- JavaScript Objects
- JavaScript Events
- JavaScript Strings
- JavaScript String Methods
- JavaScript Numbers
- JavaScript Number Methods
- JavaScript Arrays
- JavaScript Array Const
- JavaScript Array Methods
- JavaScript Sorting Arrays
- JavaScript Array Iteration
- JavaScript Date Objects
- JavaScript Date Formats
- JavaScript Get Date Methods
- JavaScript Set Date Methods
- JavaScript Math Object
- JavaScript Random
- JavaScript Booleans
- JavaScript Comparison And Logical Operators
- JavaScript If Else And Else If
- JavaScript Switch Statement
- JavaScript For Loop
- JavaScript Break And Continue
- JavaScript Type Conversion
- JavaScript Bitwise Operations
- JavaScript Regular Expressions
- JavaScript Errors
- JavaScript Scope
- JavaScript Hoisting
- JavaScript Use Strict
- The JavaScript This Keyword
- JavaScript Arrow Function
- JavaScript Classes
- JavaScript JSON
- JavaScript Debugging
- JavaScript Style Guide
- JavaScript Best Practices
- JavaScript Common Mistakes
- JavaScript Performance
- JavaScript Reserved Words
- JavaScript Versions
- JavaScript History
- JavaScript Forms
- JavaScript Validation API
- JavaScript Objects
- JavaScript Object Properties
- JavaScript Function Definitions
- JavaScript Function Parameters
- JavaScript Function Invocation
- JavaScript Closures
- JavaScript Classes
- Java Script Async
- JavaScript HTML DOM
- The Browser Object Model
- JS Ajax
- JavaScript JSON
- JavaScript Web APIs
- JS Vs JQuery
JavaScript Const
ECMAScript 2015
let and const are two important new JavaScript keywords introduced by ES2015.
Const variables behave similarly to let variables, except that they cannot be reassigned:
PI = 3.14; // This will give an error
PI = PI + 10; // This will also give an error
Block Scope
In terms of Block Scope, declaring a variable with const is equivalent to letting it be declared with let.
In this example, the x declared within the block is distinct from the x declared outside the block:
// Here x is 10
{
const x = 2;
// Here x is 2
}
// Here x is 10
Assigned When Declared
When declared, JavaScript constant variables must be assigned a value:
Incorrect
PI = 3.14159265359;
Correct
Not Real Constants
The keyword consist is slightly deceptive.
It does NOT define a constant value. It defines a constant reference to a value.
Because of this, we cannot modify constant primitive values, but we can modify constant object properties.
Primitive Values
If we assign a primitive value to a constant, we cannot change the primitive value:
PI = 3.14; // This will give an error
PI = PI + 10; // This will also give an error
Constant Objects Can Change
You can change the properties of a constant object:
const car = {type:"Fiat", model:"500", color:"white"};
// You can change a property:
car.color = "red";
// You can add a property:
car.owner = "Johnson";
car = {type:"Volvo", model:"EX60", color:"red"}; // ERROR
Browser Support
Internet Explorer 10 and earlier does not support the const keyword.
The following table lists the initial browser versions to support the const keyword fully.
Chrome 49 | IE 11 / Edge | Firefox 36 | Safari 10 | Opera 36 |
Mar, 2016 | Oct, 2013 | Feb, 2015 | Sep, 2016 | Mar, 2016 |
Redeclaring
Redeclaring a JavaScript var
variable is allowed anywhere in a program:
var x = 3; // Allowed
x = 4; // Allowed
Redeclaring or reassigning an existing
var
or let
variable to const
, in the same scope, or in the same block, is not allowed:
const x = 2; // Not allowed
{
let x = 2; // Allowed
const x = 2; // Not allowed
}
Redeclaring or reassigning an existing
const
variable, in the same scope, or in the same block, is not allowed:
const x = 3; // Not allowed
x = 3; // Not allowed
var x = 3; // Not allowed
let x = 3; // Not allowed
{
const x = 2; // Allowed
const x = 3; // Not allowed
x = 3; // Not allowed
var x = 3; // Not allowed
let x = 3; // Not allowed
}
Redeclaring a variable with
const
, in another scope, or in another block, is allowed:
{
const x = 3; // Allowed
}
{
const x = 4; // Allowed
}
Const Hoisting
Variables declared with var are elevated and can be initialised at any point.
You can use the variable prior to its declaration:
var carName;
Examine the chapter JavaScript Hoisting if you wish to learn more about hoisting.
Const variables are also promoted to the top, but they are not initialised.
Utilizing a const variable prior to its declaration will result in a ReferenceError.
const carName = "Volvo";
Products
Partner
Copyright © RVR Innovations LLP 2024 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP