liveBook · Manning

Syntactic sugar

As explained in Secrets of the JavaScript Ninja, Second Edition

Language syntax that makes an operation easier to read or write without adding new underlying capabilities.

Syntactic sugar makes familiar operations shorter or clearer to write without adding underlying capabilities. Compare the surface syntax with the operation it abbreviates to understand what the runtime is doing. section 7.4.1 section 6.2.1

JavaScript uses syntactic sugar for several different tasks:

  • ES6 classes provide a more elegant way to write prototype-based objects; equivalent ES5 code uses constructor functions and prototype methods. section 7.4.1
  • Arrow functions shorten function expressions, as in a sorting callback written with `(value1, value2) => value1 - value2`. section 3.3.2
  • A for-of loop handles iterator results behind the scenes instead of repeatedly calling `next()` and checking `done`. section 6.2.1
Listing 7.13. Creating a class in ES6
class Ninja{                                         #A
  constructor(name){                                 #B
    this.name = name;                                #B
  }                                                  #B

  swingSword(){                                      #C
    return true;                                     #C
  }                                                  #C
}

var ninja = new Ninja("Yoshi");                      #D

assert(ninja instanceof Ninja, "Our ninja is a Ninja");     #E
assert(ninja.name === "Yoshi", "named Yoshi");              #E
assert(ninja.swingSword(), "and he can swing a sword");     #E

See a Ninja class, instance creation, and checks of its properties and method. section 7.4.1

var values = [0, 3, 2, 5, 7, 4, 8, 1];
values.sort((value1,value2) => value1 – value2);


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":1,\"ch\":12},{\"line\":1,\"ch\":46}]]"}
!@%STYLE%@!

Compare a sorting callback written as a function expression and as an arrow function. section 3.3.2

for(var item of WeaponGenerator ()){
  assert(item !== null, item);
}

See a for-of loop consume values from a generator. section 6.2.1

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Defines the idea through three comparisons, including class-to-prototype mechanics and iterator next/done handling.
Examples
Uses a Ninja class, a sorting callback, and a generator loop to compare concise syntax with its mechanics.
What you can do after
Understand how class, arrow-function, and for-of forms relate to prototypes, function expressions, and iterator operations.
How it connects
Connects classes with prototypes, arrow functions with function expressions, and for-of loops with iterators.

Also explained in

Compare how 2 books explain syntactic sugar →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Second Edition. The section links point to the parts of the book it draws on.