JavaScript Features

Page 2

Introduction Keyword let Hoisting Good Practice Summary ECMAScript 6: Page 1 Code Examples: Page 1

Introduction

This page discusses the ECMAScript 6 (ES6) keyword, let, how hoisting affects JavaScript variables and concludes with some ideas for good practice.

The keyword, let, allows developers to scope variables to blocks. The term hoist, means by default, JavaScript moves variable declarations up to the top. In a sense, let and hoisting are opposites. The keyword, let, provides tighter control of variable scope. Hoisting allows looser control of variable declarations. It's good practice to block scope variables and avoid hoisting.

Keyword Let

The keyword, let, allows block scoped variables without hoisting. Before ES6, JavaScript enabled global and function level variable scoping, only. Use let as follows to declare variable, j, for access only within the for loop. Variable j cannot be modified or accessed outside of the loop. The next section discusses hoisting.

for(let j = 0; j < 4; j++){
 eDebug.innerHTML += "j: "+j+",";
}

Hoisting

JavaScript allows developers to use a variable, and later declare the variable. Hoisting, by default, relocates all variable declarations to the top. For example the following code works, because JavaScript moves the declaration up. Assume the developer wrote the following code.

sName = "Veronica";
var sName;

JavaScript rearranges the declaration. JavaScript sees the following code.

var sName;
sName = "Veronica";

Good Practice

Avoid confusion with hoisting. Declare all variables at the top of a file and use strict mode to avoid hoisting.

Add the following line to the top of a script or function. Strict mode rejects bad syntax, such as the ability to use a variable before its declaration.

"use strict";

Prevent side effects related to unexpected variable use. Use the keyword, let, for block level variables.

Summary

This page discussed the ECMAScript 6 (ES6) keyword, let, how hoisting affects JavaScript variables and concluded with some ideas for good practice.

The keyword, let, allows developers to scope variables to blocks. The term hoist, means by default, JavaScript moves variable declarations up to the top. In a sense, let and hoisting are opposites. The keyword, let, provides tighter control of variable scope. Hoisting allows looser control of variable declarations. It's good practice to block scope variables and avoid hoisting.

Tags

JavaScript, canvas drawing, web developer, web design, website design, web designing course, how to become a web developer, coding websites, website developers,learning web design,html web design, html5 canvas tutorials, coding Website, html5 canvas, learn to code, javascript tutorial,html5 canvas tutorial,learn html tutorial, simple html tutorial
Copyright © 2015 Seven Thunder Software. All Rights Reserved.