New possibilities of JavaScript and how to use them
SYLWIA LASKOWSKA
JavaScript
EcmaScript
JavaScript
Created by Brendan Eich in 1995
Ecma-262 standard
JScript
ActionScript
2009 - ES5
2015 - ES6
2016 - ES2016,
2017 - ES2017,
2018 - ES2018,
2019 - ES2019,
....
Technical Committee 39
JavaScript developers, implementers, academics, and more, collaborating with the community to maintain and evolve the definition of JavaScript.
All major browser vendors.
Meet 6 times a year.
Syntax:
var boolean = arr.includes(searchElement[,fromIndex])
myFavoriteBeers = ['Pearl', 'TurboBeer', 'BeerScript']
myFavoriteBeers.includes('TurboBeer');
// true
myFavoriteBeers.includes('kitty');
// false
myFavoriteBeers.includes('BeerScript', 1);
// true
myFavoriteBeers.includes('Pearl', 5);
// false
const beerPack = 4;
let beerPackChain = Math.pow(beerPack, 2);
// is now
beerPackChain = beerPack ** 2 // 16
Callback Hell (ES5)
const userCity = 'https://ourfakeapi.pl/usercity';
const bestBeerInTheCity = 'https://ourfakeapi.pl/city?name=';
const beerFans = 'https://ourfakeapi.pl/fans?beer=';
function getBeerFans() {
request(userCity, function(result) {
request(bestBeerInTheCity + result), function(result) {
request(beerFans + result), function(result) {
console.log("Help!!! How did we end up like this???");
}
}
})
}
Promises (ES6)
const userCity = 'https://ourfakeapi.pl/usercity';
const bestBeerInTheCity = 'https://ourfakeapi.pl/city?name=';
const beerFans = 'https://ourfakeapi.pl/fans?beer=';
function getBeerFans() {
return axios.get(userCity)
.then((response) => {
return axios.get(`bestBeerInTheCity${response}`);
})
.then((response) => {
return axios.get(`beerFans${response}`)
})
.then((response) => {
console.log('Ok, not so bad!');
});
}
Async - await (ES8)
const userCity = 'https://ourfakeapi.pl/usercity';
const bestBeerInTheCity = 'https://ourfakeapi.pl/city?name=';
const beerFans = 'https://ourfakeapi.pl/fans?beer=';
async function getBeerFans() {
const city = await axios.get(userCity);
const bestBeer = await axios.get(`bestBeerInTheCity${city}`);
const fans = await axios.get(`beerFans${bestBeer}`);
console.log('That is all code we need!');
}
let beerPrices = {
beer1: 5,
beer2: 6,
beer3: 8
}
let beersIntoEntries = Object.entries(beerPrices);
// [['beer1', 5], ['beer2', 6], ['beer3', 8]]
let priceInCapitalCity = beersIntoEntries
.map(([key, value]) => [key, value * 2]);
// [['beer1', 10], ['beer2', 12], ['beer3', 16]]
let beers = {
beer1: 'white',
beer2: 'brown',
beer3: 'yellow'
}
let beerColors = Object.values(beers);
// ['white', 'brown', 'yellow']
let adv = `Our beers are: ${beerColors.join(', ')}`;
// Our beers are: white, brown, yellow
favoriteBeer = {
name: 'Gold Tiger',
type: 'stout',
logName: () => console.log(this.name)
}
Object.getOwnPropertyDescriptors(favoriteBeer);
/*
{name: {…}, type: {…}, logName: {…}}
logName: {value: ƒ, writable: true, enumerable: true, configurable: true}
name: {value: "Gold Tiger", writable: true, enumerable: true, configurable: true}
type: {value: "stout", writable: true, enumerable: true, configurable: true}
__proto__: Object
*/
favoriteBeer = {
name: 'Gold Tiger',
type: 'stout',
set description(desc) {
console.log(desc);
}
}
const favoriteBeer2 = Object.assign({}, favoriteBeer);
// {name: "Gold Tiger", type: "stout", description: undefined}
const favoriteBeer3 = Object.defineProperties({},
Object.getOwnPropertyDescriptors(favoriteBeer));
// {name: "Gold Tiger", type: "stout"}
favoriteBeer3.description = 'It is my favorite beer!';
// It is my favorite beer!
Shared memory and atomics
Trailing commas
String.prototype.padStart/padEnd
let {name, id, ...remaining} = {
name: 'Sylwia',
id: '123456',
favouriteBeer: 'Bar Free',
city: 'Gdansk',
age: 60
}
name; // Sylwia
id; // '123456'
remaining; // {favouriteBeer: 'Bar Free', city: 'Gdansk', age: 60}
const data = {name: 'Sylwia', id: '123456'};
const favourites = {favBeer: 'Xxx', favBar: 'The Cheapest'};
const oneBigObj = {...data, ...favourites};
// {name: 'Sylwia', id: '123456', favBeer: 'Xxx', favBar: 'The Cheapest'}
let loading = true;
axios.get('https://somefakeapiagain.pl/myFavouriteBeer')
.then(resp => doSthWithResp(resp))
.catch(err => handleError(err))
.finally(() => {
loading = false;
// This will always be executed
})
Async iterators
RegExp named capture groups
RegExp unicode property escapes
RegExp lookbehind assertions
s (dotAll) flag for regular expressions
Template literal revision
let nestedBeerArray = ['beer1', 'beer2', ['beer3', 'beer4']];
nestedBeerArray.flat();
// ['beer1', 'beer2', 'beer3', 'beer4']
let beerIndexes = [1, 2, [3, 4, [5, 6, [7, 8]]]];
beerIndexes.flat(2);
// [1, 2, 3, 4, 5, 6, [7, 8]];
beerIndexes.flat(Infinity);
// [1, 2, 3, 4, 5, 6, 7, 7];
const normalBeers = ['Pulsner', 'Golden Tiger'];
let normalAndLight = normalBeers.map(beer => [beer, `${beer} Light`]));
// [['Pulsner', 'Pulsner Light'], ['Goldern Tiger', 'Golden Tiger Light']];
normalAndLight = normalBeers.flatMap(beer => [beer, `${beer} Light`]));
// ['Pulsner', 'Pulsner Light', 'Goldern Tiger', 'Golden Tiger Light'];
let beerPrices = {
beer1: 5,
beer2: 6,
beer3: 8
}
let beersIntoEntries = Object.entries(beerPrices);
// [['beer1', 5], ['beer2', 6], ['beer3', 8]]
let priceInCapitalCity = beersIntoEntries.map(([key, value]) => [key, value * 2]);
// [['beer1', 10], ['beer2', 12], ['beer3', 16]]
// LET THE MAGIC HAPPEN!!!
let doublePriceObj = Object.fromEntries(priceInCapitalCity);
{
beer1: 10,
beer2: 12,
beer3: 16
}
String.prototype.trimStart
String.prototype.trimEnd
Optional catch binding
Symbol.prototype.description
Array.prototype.sort() is now required to be stable
Well-formed JSON.stringify
JSON superset
Function.prototype.toString revision
Dynamic import()
BigInt
Private class field
Who are your clients?
What browsers do you need to support?
Polyfills/transpilers to the rescue