var and let and Const__javascript

Source: Internet
Author: User

var

keywords used to declare variables in ES5, no block-level scopes, variable elevation
Let

ES6, there is a block-level scope, there is no variable elevation, so if the first use of the declaration will appear temporary Dead zone
Const

ES6, there is a block-level scope, no variable ascension, the definition must be assigned an initial value, the variable point to the address can not be changed


another difference between the Var and let

Let cannot be defined as a global variable, that is, through window. cannot be accessed, but Var can.

	Example 1: through var
		var sum = 0 for
		(var i = 0; i< i++) {
			sum = sum + i
		}
		console.log (i)//*
		Conso Le.log (window.sum)//
		Console.log (this.sum)//
		
	2: Through let
		-let sum = 0 for (let
		i = 0; i<) +) {
			sum = sum + i
		}
		console.log (i)//undefined
		console.log (window.sum)//undefined
		Console.log (this.sum)//undefined


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.