A.1. New in ES6

Since 2015, ECMAScript 6 has offered new syntax and conventions for developing with JavaScript. For that reason, this book covers some of the ES6 keywords and formats you’ll use. Keywords are terms that have a reserved meaning in JavaScript and are used to provide the syntax and interpretability of your code.

A.1.1. The let keyword

You’re probably used to declaring variables with the var keyword. With ES6, it’s more appropriate to use the let keyword to define variables as they apply to a specific scoped block. Until a variable is defined within a particular block of code, you can’t access it.

A let variable defined in an if block can’t be accessed outside the block, for example, whereas a var variable is scoped to the function within which it’s defined, as shown in the next listing.

Listing A.1. Example use of the let keyword
function sample() {
  var num = 60;
  if (num > 50){
    let num = 0;
  }
  console.log(num);
}

Because let variables are scoped to code blocks beyond functions, they could be global variables to a module or an entire application. As a result, let gives variable definition more security and is preferred to var.

Note

When using "use strict"; you can’t redefine the same let variable, whereas you can with var.

A.1.2. The const variable

A const variable can’t be reassigned. Typically, you should use this keyword in place of let for variables whose values you don’t expect to manipulate in your code. This guideline can also apply to loading libraries or modules in Node.js, as you see in unit 1. If you try to reassign a const variable, you get a Duplicate Declaration Error.

The code in the next listing crashes because a new let variable is being declared with the name of an existing constant.

Listing A.2. Example use of the const variable
function applyDiscount(discountPrice) {
  const basePrice = 1000;
  let basePrice = basePrice - discountPrice;
  console.log(basePrice);
}

A.1.3. String interpolation

Until ES6, to print or log a variable’s value within a string, you had to append the string around the variable, as shown in the following listing.

Listing A.3. Example of string concatenation
var x = 45;
console.log("It is " + x + " degrees outside!");

With ES6, you can use backticks (`) and ${} to interpolate variables into a string, as shown in the next listing.

Listing A.4. Interpolating strings with backticks
var x = 45;
console.log(`It is ${x} degrees outside!`);

The resulting code is cleaner, easier to read, and easier to edit.

A.1.4. Arrow functions

Arrow functions are one way that ES6 is making code more succinct and easier to read. With the => arrow symbol and a change in the conventional function syntax, you can turn a multiline function into one line. Take the example in the following listing.

Listing A.5. Defining a function with the function keyword
function printName(name) {
  console.log(`My name is ${name}`);
}

You can rewrite this code as shown in the next listing.

Listing A.6. Defining an arrow function
let printName = name => console.log(`My name is ${name}`);

More important, arrow functions in ES6 preserve the this variable from its outer scope, as shown in the following listing.

Listing A.7. Example use of the this keyword within functions
let dog = {
  name: "Sparky",
  printNameAfterTime: function() {
    setTimeout(function() {
      console.log(`My name is ${this.name}`);
    }, 1000);
  }
}

In this example, dog.printNameAfterTime() prints My name is undefined because this .name is out of the setTimeout function scope despite the assumption that this refers to the dog object. With arrow functions, however, this persists within the setTimeout function, as shown in the next listing.

Listing A.8. Example use of the this keyword with arrow functions
let dog = {
  name: "Sparky",
  printNameAfterTime() {
    setTimeout(() => {
      console.log(`My name is ${this.name}`);
    }, 1000);
  }
}

Now you can print My name is Sparky, and the code is more compact!

To succeed with Node.js, you need to succeed with JavaScript in general. Because Node.js requires sufficient knowledge of some core JavaScript and programming concepts, this lesson reviews what you need to know to get started. If you haven’t had much experience with JavaScript, I recommend reading Secrets of the JavaScript Ninja, Second Edition by John Resig and Bear Bibeault (Manning, 2016).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset