- 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 Let
ECMAScript 2015
let and const are two important new JavaScript keywords introduced by ES2015.
These two keywords provide variables (and constants) with Block Scope in JavaScript.
Prior to ES2015, JavaScript only supported two types of scope: global and function.
Global Scope
Variables declared Globally (outside any function) have Global Scope.
// code here can use carName
function myFunction() {
// code here can also use carName
}
JavaScript programmes can access global variables from anywhere.
Function Scope
Variables declared Locally (inside a function) have Function Scope.
function myFunction() {
var carName = "Volvo";
// code here CAN use carName
}
// code here can NOT use carName
Local variables can only be accessed from within the function declaration.
JavaScript Block Scope
The var keyword-declared variables cannot have Block Scope.
Declared variables within a block can be accessed from outside the block.
var x = 2;
}
// x CAN be used here
Prior to ES2015, JavaScript lacked Block Scope.
The let keyword allows variables to have Block Scope.
Variables declared within a block are inaccessible from outside the block:
let x = 2;
}
// x can NOT be used here
Redeclaring Variables
Redeclaring a variable with the var keyword can cause complications.
When a variable is redeclared within a block, it is also redeclared outside the block:
// Here x is 10
{
var x = 2;
// Here x is 2
}
// Here x is 2
Using the let keyword to redeclare a variable can solve this problem.
A variable that is redeclared within a block is not redeclared outside the block:
???????
// Here x is 10
{
let x = 2;
// Here x is 2
}
// Here x is 10
Browser Support
Internet Explorer 11 and earlier versions do not fully support the let keyword.
The following table lists the initial browser versions to support the let keyword fully.
Chrome 49 | Edge 12 | Firefox 44 | Safari 11 | Opera 36 |
Mar, 2016 | Jul, 2015 | Jan, 2015 | Sep, 2017 | Mar, 2016 |
Loop Scope
Using var
in a loop:
for (var i = 0; i < 10; i++) {
// some statements
}
// Here i is 10
Using
let
in a loop:
for (let i = 0; i < 10; i++) {
// some statements
}
// Here i is 5
In the first example, the variable declared within the loop is redeclared outside of the loop.
In the second example, the variable declared within the loop is not redeclared outside the loop when let is used.
When let is used to declare the I variable within a loop, I is only visible within the loop.
Function Scope
Variables declared with var
and let
are quite similar when declared inside a function.
They will both have Function Scope:
var carName = "Volvo"; // Function Scope
}
let carName = "Volvo"; // Function Scope
}
Global Scope
When declared outside a block, variables declared with var and let are very similar.
They will both have Global Scope:
Global Variables In HTML
The global scope of JavaScript is the JavaScript environment.
The global scope in HTML is the window object.
Variables defined globally with the var keyword are members of the window object.
// code here can use window.carName
???????
// code here cannot use window.carName
Redeclaring
Redeclaring a JavaScript variable with var
is allowed anywhere in a program:
// Now x is 2
var x = 3;
// Now x is 3
Redeclaring a
var
variable with let
, in the same scope, or in the same block, is not allowed:
let x = 3; // Not allowed
{
var x = 4; // Allowed
let x = 5 // Not allowed
}
Redeclaring a
let
variable with let
, in the same scope, or in the same block, is not allowed:
let x = 3; // Not allowed
{
let x = 4; // Allowed
let x = 5; // Not allowed
}
Redeclaring a
let
variable with var
, in the same scope, or in the same block, is not allowed:
var x = 3; // Not allowed
{
let x = 4; // Allowed
var x = 5; // Not allowed
}
Redeclaring a variable with
let
, in another scope, or in another block, is allowed:
{
let x = 3; // Allowed
}
{
let x = 4; // Allowed
}
Let Hoisting
Variables declared with var are elevated and can be initialised at any point.
You can use the variable prior to its declaration:
alert(carName);
var carName;
Examine the chapter JavaScript Hoisting if you wish to learn more about hoisting.
Let variables are also elevated to the top of the block, but they are not initialised.
Using a let variable prior to its declaration will generate a ReferenceError:
let carName = "Volvo";
COMPANY
Products
Partner
Copyright © RVR Innovations LLP 2024 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP