ECMeowScript - What’s New in JavaScript Explained with Cats

A presentation at Code://Remote 2020 in August 2020 in by Tomomi ❤ Imura

Slide 1

Slide 1

ECMeowScript What’s new in JavaScript Explained with Cats @Web Directions Code://Remote 2020 Tomomi Imura @girlie_mac 🐱-as-a-Service

Slide 2

Slide 2

Hello 👋 Tomomi is: ● ● ● ● @girlie_mac Working at Microsoft JavaScript all the things Live & work in San Francisco Cat-as-a-Service provider

Slide 3

Slide 3

Cat-as-a-Service 🐱aaS is a development and distribution model using cats to make it more enjoyable @girlie_mac

Slide 4

Slide 4

Cat-as-a-Service: HTTP Status Cats @girlie_mac 🔗 https://http.cat (not my site, but I let the owner to park my images because the TLD is awesome)

Slide 5

Slide 5

Cat-as-a-Service: Raspberry Pi KittyCam @girlie_mac https://github.com/girliemac/RPi-KittyCam

Slide 6

Slide 6

Cat-as-a-Service: Git Purr @girlie_mac 🔗 https://girliemac.com/blog/2017/12/26/git-purr/

Slide 7

Slide 7

Yay, JavaScript! @girlie_mac

Slide 8

Slide 8

Yay, EcmaScript! @girlie_mac

Slide 9

Slide 9

Yay, EcmaScript with cats! @girlie_mac

Slide 10

Slide 10

ECMeowScript! @girlie_mac

Slide 11

Slide 11

WTF! Meow @girlie_mac Image credit: @tikkafromeast (Instagram)

Slide 12

Slide 12

ES6 / ES2015 @girlie_mac

Slide 13

Slide 13

ES6 / ES2015: Major Changes & Improvement @girlie_mac

Slide 14

Slide 14

ES6 /ES2015: In a nutshell ● Arrow functions ● const & let (block scope, vs. ver as function scope) ● Template literals ○ Expressions interpolation Hi, ${cat.name} ○ String interpolation (multi-lines without annoying \n +) etc. ● Promises ● Iterators & generators (function*) ● for..of iterator @girlie_mac

Slide 15

Slide 15

ES6 /ES2015: In a nutshell ● Class definition & inheritance ● Destructuring assignment const {name, age, breed} = cat ● Rest parameter & Spread operator (“Three dots”) …cats ● Set object ● Map object (“dictionary”) Also: ● Internationalization API — e.g. Intl.DateTimeFormat() @girlie_mac

Slide 16

Slide 16

�� My article on the ECMA-402 International API, Intl.DateTimeFormat https://girliemac.com/blog/2019/04/02/ javascript-i18n-reiwa-era/ @girlie_mac

Slide 17

Slide 17

ES6 /ES2015: Examples const cats = [ {name: ‘Jamie’, type: ‘tabby’, img: }, {name: ‘Leia’, type: ‘tuxedo’, img: }, {name: ‘Alice’, type: ‘tortoiseshell’, img: ]; @girlie_mac }

Slide 18

Slide 18

ES6 /ES2015: Examples const cats = [ {name: ‘Jamie’, type: ‘tabby’, img: ‘00_jamie.jpg’}, {name: ‘Leia’, type: ‘tuxedo’, img: ‘01_leia.jpg’}, {name: ‘Alice’, type: ‘tortoiseshell’, img: ‘02_alice.jpg’} ] From the given array above, create a new array, catNames [‘Jamie’, ‘Leia’, ‘Alice’] @girlie_mac

Slide 19

Slide 19

ES6 /ES2015: Examples Pre-ES6 D var catNames = []; cats.forEach(function(cat) { catNames.push(cat.name); }); // catNames is [‘Jamie’, ‘Leia’, ‘Alice’]; @girlie_mac

Slide 20

Slide 20

ES6 /ES2015: Array.prototype.map() .map() creates a new array from calling a function on each item in the original array. const nums = [1, 2, 3, 4]; const newNums = nums.map(function(n) { return n * 2; }); // [2, 4, 6, 8] @girlie_mac Map calls a callback on each element in an array!

Slide 21

Slide 21

ES6 /ES2015: Array.prototype.map() with ES6, array.map() const catNames = cats.map(function(cat) { return cat.name; }); // [‘Jamie’, ‘Leia’, ‘Alice’]; @girlie_mac

Slide 22

Slide 22

ES6 /ES2015: Arrow Function write it simpler with with ES6 Arrow function const catNames = cats.map((cat) => { return cat.name; }); // [‘Jamie’, ‘Leia’, ‘Alice’]; @girlie_mac

Slide 23

Slide 23

ES6 /ES2015: Arrow Function (Concisely!) Even simpler with ES6 concise arrow function syntax! const catNames = cats.map(cat => cat.name); // [‘Jamie’, ‘Leia’, ‘Alice’]; @girlie_mac

Slide 24

Slide 24

ES6 /ES2015: Destructuring Alternatively with ES6 Object destructuring const catNames4 = cats.map(({name}) => name); // [‘Jamie’, ‘Leia’, ‘Alice’]; @girlie_mac extract { name } property out of the cats object

Slide 25

Slide 25

ES6 /ES2015: Destructuring const cat01 = { name: ‘Jamie’, type: ‘tabby’, img: ‘001.jpg’ }; const {name01, type01, img01} = cat01; // Pre-ES6 var name01 = cat01.name; var type01 = cat01.type; var img01 = cat01.img; High-five! @girlie_mac

Slide 26

Slide 26

ES6 / ES2015: Spread operator “Three-dots” Spread operator allows an iterable (array or string) to be expanded. e.g. concat arrays: catNames = [‘Jamie’, ‘Leia’, ‘Alice’]; const newCatNames = […catNames, ‘Yugi’,’Snori’]; // newCatNames is [‘Jamie’,’Leia’,’Alice’,’Yugi’,’Snori’] @girlie_mac

Slide 27

Slide 27

Concat const coats1 = [‘solid’, ‘bicolor’, ‘tabby’]; const coats2 = [‘calico’, ‘tortoiseshell’]; Three-dots Tricks const coats = […coats1, …coats2]; // [‘solid’, ‘bicolor’, ‘tabby’, ‘calico’, ‘tortoiseshell’] Convert a string to array const str = ‘kitty’; girliemac.com/blog/ 2020/06/18/ javascript-spread-operator const newArr = […str]; // [‘k’, ‘i’, ‘t’, ‘t’, ‘y’]; Find the largest const catWeights = [10, 9, 6, 12]; const maxWeight = Math.max(…catWeights); // 12 Remove duplicates from an array const allCatNames =[‘chewie’, ‘leia’, ‘yoda’, ‘chewie’, ‘luke’, ‘leia’]; const catNames = […new Set(allCatNames)]; // [‘chewie’, ‘leia’, ‘yoda’, ‘luke’];

Slide 28

Slide 28

ES6 / ES2015: Map Yay, JS HashMap! Let’s catalog a collection of cats! Key Value const map = new Map(); map.set(‘Jamie’, 1234); ‘Jamie’ 1234 map.set(‘Leia’, 1455); ‘Leia’ 1455 map.set(‘Alice’, 3456); ‘Alice’ 3456 console.log(map); // {“Jamie” => 1234, “Leia” => 1455, “Alice” => 3456} map.has(‘Jamie’); // true map.get(‘Jamie’); // 1234 @girlie_mac

Slide 29

Slide 29

ES7 / ES2016 @girlie_mac

Slide 30

Slide 30

ES7 / ES2016: In a Nutshell ● Exponentiation Operator ● Array.prototype.includes() @girlie_mac

Slide 31

Slide 31

ES7 / 2016: Exponentiation Operator ** a @girlie_mac * * b

Slide 32

Slide 32

ES7 / 2016: Exponentiation Operator ** The exponentiation operator returns the result of raising the first operand (var a) to the power of the second operand (var b). So: a ** b is same as 3 Math.paw(a, b) o // 3 ** 2 returns @girlie_mac �� 2 9

Slide 33

Slide 33

ES7 / ES2016: Array.prototype.includes() const coatColors = [‘solid’, ‘bicolor’, ‘tabby’, ‘calico’, ‘tortoiseshell’]; coatColors.includes(‘tabby’) // true coatColors.includes(‘neon’) // false To do the same operations with indexOf() coatColors.indexOf(‘tabby’) > -1 // true coatColors.indexOf(‘neon’) > -1 // false @girlie_mac s✔ ude incl

Slide 34

Slide 34

ES8 / ES2017 @girlie_mac

Slide 35

Slide 35

ES8 / ES2017: In a Nutshell ● Async / await ● padStart() and padEnd() ● Trailing commas ● Object.entries() - returns an object w/ key-value pairs in an array as an array of arrays ● Object.values() - returns an array of a given object’s enumerable property values ● Shared memory and atomics @girlie_mac

Slide 36

Slide 36

ES8 /ES2017: Async & Await An async function with await expression: ● is a syntactic sugar on top of promises ● pauses the execution of the async function to wait for the passed Promise’s resolution ● makes the async code look like synchronous code (= simpler!) @girlie_mac

Slide 37

Slide 37

ES8 /ES2017: Async & Await litter-robot.com @girlie_mac https://youtube.com/watch?v=zZHeZ25NFso

Slide 38

Slide 38

ES8 /ES2017: Async & Await const runLitterRobot = async() => { await cat.poop(); clean(); }; @girlie_mac cat.poop() clean()

Slide 39

Slide 39

ES8 /ES2017: Async & Await const runLitterRobot = () => { cat.poop(); � clean(); }; What can it poosibly go wrong? @girlie_mac � � � � �

Slide 40

Slide 40

ES8 /ES2017: String Padding String.prototype.padStart() and padEnd() const boxStr = ‘📦’ @girlie_mac

Slide 41

Slide 41

ES8 / ES 2017: String Padding Let’s make a string, total 4 chars long, with some paddings in the beginning of the string. @girlie_mac

Slide 42

Slide 42

ES8 / ES 2017: String Padding boxStr.padStart(4, ‘🐱’); // boxStr is now (‘🐱🐱🐱📦’) @girlie_mac

Slide 43

Slide 43

ES8 / ES 2017: String Padding Example with padStart() to pad out a value with leading 0 const str = ‘5’; str.padStart(4, ‘0’); // ‘0005’ 📂 0005.jpg @girlie_mac

Slide 44

Slide 44

ES8 / ES 2017: String Padding padStart() and padEnd() work as expected with RtL strings too. (This is why the methods are not called padLeft/Right!) const strArabic = ‘‫’أﻧﺎ ﻗطﺔ‬ strArabic.padEnd(10,’‫;)’ﻗط‬ // ‘‫’أﻧﺎ ﻗطﺔﻗطﻘطﻘطﻘط‬ @girlie_mac “I am a cat”

Slide 45

Slide 45

ES8 / ES 2017: Trailing comma in Func Param (param,) Not allowed in pre-ES8 @girlie_mac

Slide 46

Slide 46

ES8 / ES 2017: Trailing comma in Func Param (param,) @girlie_mac

Slide 47

Slide 47

ES8 / ES 2017: Trailing comma in Func Param In ES5: ☑ Trailing comma in object literals In ES8: ☑ Trailing comma in function parameter list & calls const makeFood = (param1, param2,) => {..}; makeFood(‘tuna’, ‘salmon’,); Approved! @girlie_mac

Slide 48

Slide 48

ES9 / ES2018 @girlie_mac

Slide 49

Slide 49

ES9 / ES2018: In a Nutshell ● Spread & Rest properties - now you can use the three-dots with objects! ● RegEx improvements ● Asynchronous iteration ● Promise finally() fetch(‘https://api.github.com/users/octocat’) .then(result => {···}) .catch(error => {···}) .finally(() => {console.log(‘🐙🐱’)}); @girlie_mac

Slide 50

Slide 50

ES10 / ES2019 @girlie_mac

Slide 51

Slide 51

ES10 / ES2019: In a Nutshell ● String.prototype.trimStart() and trimEnd() ● Array.prototype.flat() and flatMap() ● Object.prototype.fromEntries() ● Function.prototype.toString() ● Well-formed JSON.stringify() ● Better & faster array sort() @girlie_mac

Slide 52

Slide 52

ES2019: String.prototype.trimStart() & trimEnd() Chewy.com @girlie_mac

Slide 53

Slide 53

ES2019: String.prototype.trimStart() Snip! @girlie_mac

Slide 54

Slide 54

ES2019: String.prototype.trimStart() The trimStart() removes whitespace from the beginning of a string const greetStr = ’ meow ‘; greetStr.trimStart(); // ‘meow @girlie_mac ‘

Slide 55

Slide 55

ES2019: String.prototype.trimEnd() Snip! @girlie_mac

Slide 56

Slide 56

ES2019: String.prototype.trimEnd() The trimEnd() removes whitespace from the end of a string const greetStr = ’ meow greetStr.trimEnd(); // ’ @girlie_mac ‘; meow’ Thanks, Szabolcs Szabolcsi-Toth (@_Nec) for the initial ideas!

Slide 57

Slide 57

ES2019: Array.prototype.flat() & flatMap() GIF: Maru flattens himself (https://sisinmaru.com/) @girlie_mac

Slide 58

Slide 58

ES2019: Array.prototype.flat() const colors = [[‘black’, ‘gray’, [‘orange’, ‘light orange’]], ‘bicolor’, ‘calico’]; const colors1 = colors.flat(); // [“black”, “gray”, Array(2), “bicolor”, “calico”] // Optional depth level const colors2 = colors.flat(2); // [“black”, “gray”, “orange”, “light orange”, “bicolor”, “calico”] @girlie_mac

Slide 59

Slide 59

ES11 / ES2020 @girlie_mac

Slide 60

Slide 60

ES11 / ES2020: The Latest ● ● ● ● ● ● ● ● @girlie_mac BigInt globalThis Dynamic import Nullish coalescing operator, ?? Optional chaining, ? Promise.allSettled() String.prototype.matchAll() Module export

Slide 61

Slide 61

ES2020: BigInt JavaScript has two types of number types: ● Number (largest number is Number.MAX_SAFE_INTEGER, 253 - 1) n ● BigInt allows you to use even bigger number! I can count only up to 253 - 1 @girlie_mac “Max” the cat BigCat

Slide 62

Slide 62

ES2020: BigInt Use suffix, n let max = Number.MAX_SAFE_INTEGER; // 9007199254740991 max++ // 9007199254740992 n max++ // 9007199254740992 let largerThanMax = 9007199254740992n; largerThanMax++ // 9007199254740993n largerThanMax++ // 9007199254740994n largerThanMax++ // 9007199254740995n @girlie_mac “Max” the cat BigCat

Slide 63

Slide 63

ES2020: BigInt BigCat Roarrrrr! BigInt allows JavaScript devs to access the Twitter 64-bit unsigned integers IDs as numbers. 9007199254740992n @girlie_mac

Slide 64

Slide 64

Well, I’m running out of cat puns so ending this presentation impurrfectly. @girlie_mac

Slide 65

Slide 65

But I hope you are feline good about ES.next by now. @girlie_mac

Slide 66

Slide 66

Thank you, you’re pawesome! Tomomi Imura @girlie_mac