Javascript 101
2.1 ES6 Enhancements
2.1 ES6 Enhancements
There are a number of enhancements in the
ES6 specification but we will discuss on some of the most commonly used features. Not all browser will be able to support the ES6 syntax. A useful website is caniuse.com which lets you see the browser support for various different HTML, CSS, and Javascript features. For many cases, there are **polyfills** that you can include in order for older browser to work as expected with the new codes.In web development, a polyfill is code that implements a feature on web browsers that do not support the feature. Most often, it refers to a JavaScript library that implements an HTML5 web standard, either an established standard (supported by some browsers) on older browsers, or a proposed standard (not supported by any browsers) on existing browsers.
Source:
Wikipedia
Block-Scoped Variables (Recap)
We have already seen this previously through the use of
let
and const
.
Basically, we are now able to have variables that are declared with block
scoping { }
instead of function scoping.
{ //var1 is only available in this block let var1 = 10;}
//var1 is not available hereconsole.log(var1); //Error: var1 is not defined
Default Function Parameter Values (Recap)
We have also discussed
default function parameters previously.
This is achieved by using the assignment (=
) syntax.
//generate a random integer within a range from//min (inclusive) to max (exclusive)//default range: [0, 100)function generateRandomInt(min = 0, max = 100) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min;}
//generate 10 random numbers within range of 0 to 100for (let i = 0; i < 10; i++) { console.log(generateRandomInt());}
//generate 10 random numbers within range of 0 to 1000for (let i = 0; i < 10; i++) { console.log(generateRandomInt(0, 1000));}
In the rest of this section, we will elaborate on the following ES6 syntax:
- Arrow Functions
- String Enhancements
- ES6 Class System
- Object Enhancements
- Rest and Spread Operators
- Destructuring
- Promises
- Module System