Simple ES6 's Let and const command _JAVASCRIPT skills

Source: Internet
Author: User
Tags constant

Variables declared by let and Const are valid only within the code block

{Let
a = ten;
var b = 1;
}
A//REFERENCEERROR:A is not defined.
b//1

There is no variable elevation

Variables must be used after declaration, otherwise the error

var tmp = 123;
if (true) {
TMP = ' abc ';//Referenceerror let
tmp;
}

Do not allow duplicate declarations

Error
function () {Let
a = ten;
var a = 1;
}

Block-level scopes

function f () {Console.log (' I am outside! ');}
(function () {
if (false) {
///Repeat once functions f
() {console.log (' I am inside! ')}
f ();
} ());
I am inside! ES5 function Elevation
//i am outside! ES6 Block-level scopes

Const command

Declares a read-only constant, and once declared, the value of the constant cannot be changed

Once a variable is declared, it must be initialized immediately and cannot be left to be assigned later

Let command, const command, global variable declared by Class command, not part of global object's properties

var a = 1;
If in the node's REPL environment, can write GLOBAL.A//
or adopt the common method, writes THIS.A
WINDOW.A//1 let
b = 1;
WINDOW.B//undefined

I'll introduce you to ES6 's const command.

ECMA has always been the core of JS has no constant concept, es6 to make up for this one flaw;

Const foo= ' foo ';
foo= ' bar ';//typeerror:assignment to constant variable.

The example above declares a constant of the base type, such as an error if you try to modify the initial value, or if the value of the reference type is the same, but one thing to note:

Const foo=[];
foo=[1];//assignment to constant variable.

Normal error, no problem, look again:

Const foo=[1,2,3];
foo[1]=4;
Console.log (foo)//[1, 4, 3]

How come there's no error? And it can be modified? These two examples are different in that the former is modified pointer (need to familiarize with JS reference type) corresponding content changed, the latter is not pointing to still unchanged but the content of the object changed, for Foo, I'm just a pointer responsible for the corresponding object As for what the object content is, it's none of my business, so you can modify it; If you don't want the content to change, it's OK to use another method;

Const Foo=object.freeze ([1,2,3]);
foo[1]=4;
Console.log (foo)//[1, 2, 3]

So there is no need to worry about being modified;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.