Analysis of the difference between Const, VAR and let in JavaScript

Source: Internet
Author: User

There are three ways to declare variables in javascript: VAR, let, and Const. The following is a description of JS in three ways to define the variable const, VAR, let the difference.

variables defined by 1.const cannot be modified and must be initialized .

Const B = 2; // correct //  Console.log (' Outside the function const definition B: ' + B '); // with output value // B = 5; // Console.log (' Outside of the function ' modifies the const definition B: ' + B ');//Cannot output

2.var defined variables can be modified, if not initialized will output undefined, will not error.

var a = 1; // var a;//does not error Console.log (' Out of function var definition a: ' + a '); // can output a=1 function  = 4; Console.log (' var within the function defines a: ' + a '); // can output a=4 } change (); Console.log (' function call after Var definition A is the function's internal modified value: ' + a '); // can output a=4

3.let is a block-level scope, and after the function is defined with let, there is no effect on the outside of the function.

Let C = 3; Console.log (' Out of function let definition C: ' + C '); // Output c=3 function  = 6; Console.log (' within function let definition C: ' + C '); // Output c=6 } change (); Console.log (' a let definition after a function call is not affected by the internal definition of the function: ' + C '); // Output c=3

Transferred from: http://www.jb51.net/article/94495.htm

Analysis of the difference between Const, VAR and let in JavaScript

Related Article

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.