├── LICENSE ├── README.md ├── assets ├── JavaScript-Promise-Fulfilled.svg ├── JavaScript-Promise-Rejected.svg ├── promise-then-chain.svg └── star.png └── es6 ├── async-in-es6.js └── es6-features.js /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Pradeep Kumar 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JavaScript ES6 Basics 2 | 3 | > *Click ★ if you like the project. Your contributions are heartily ♡ welcome.* 4 | 5 |
6 | 7 | ## Related Interview Questions 8 | 9 | * [JavaScript Interview Questions](https://github.com/learning-zone/javascript-interview-questions) 10 | * [JavaScript Design Patterns](https://github.com/learning-zone/JavaScript-Design-Patterns) 11 | * [JavaScript Coding Practice](https://github.com/learning-zone/JavaScript-Coding-Practice) 12 | 13 |
14 | 15 | ## Table of Contents 16 | 17 | * [Introduction](#-1-introduction) 18 | * ES6 New Features 19 | * [let](#-21-let) 20 | * [let vs var](#-22-let-vs-var) 21 | * [const](#-23-const) 22 | * [Arrow Function](#-24-arrow-function) 23 | * [Default Function Parameters](#-25-default-function-parameters) 24 | * [Rest Parameter](#-26-rest-parameter) 25 | * [Spread Operator](#-27-spread-operator) 26 | * [for…of](#-28-forof) 27 | * [Binary and Octal literals](#-29-binary-and-octal-literals) 28 | * [Template literals](#-210-template-literals) 29 | * [Enhanced Object literals](#-211-Enhanced-object-literals) 30 | * Destructuring 31 | * [Array Destructuring](#-31-array-destructuring) 32 | * [Object Destructuring](#-32-object-destructuring) 33 | * ES6 Modules 34 | * [ES6 modules](#-4-es6-modules) 35 | * ES6 Classes 36 | * [Classes](#-51-classes) 37 | * [Getters and Setters](#-52-getters-and-setters) 38 | * [Class Expression](#-53-class-expression) 39 | * [Static methods](#-54-static-methods) 40 | * [Static Properties](#-55-static-properties) 41 | * [Computed property](#-56-computed-property) 42 | * [Inheritance](#-57-inheritance) 43 | * [new.target](#-58-newtarget) 44 | * Symbol 45 | * [Symbol](#-6-symbol) 46 | * Iterators & Generators 47 | * [Iterators](#-71-iterators) 48 | * [Generators](#-72-generators) 49 | * [yield](#-73-yield) 50 | * Promises 51 | * [Promises](#-81-promises) 52 | * [Promise chaining](#-82-promise-chaining) 53 | * [Promise.all()](#-83-promiseall) 54 | * [Promise.race()](#-84-promiserace) 55 | * [Promise Error Handling](#-85-promise-error-handling) 56 | * ES6 Collections 57 | * [Set](#-91-set) 58 | * [Weakset](#-92-weakset) 59 | * [Map](#-93-map) 60 | * [Weakmap](#-94-weakmap) 61 | * Array Extensions 62 | * [Array.of()](#-101-arrayof) 63 | * [Array.from()](#-102-arrayfrom) 64 | * [Array.find()](#-103-arrayfind) 65 | * [Array.findIndex()](#-104-arrayfindindex) 66 | * Object Extensions 67 | * [Object.assign()](#-111-objectassign) 68 | * [Object.is()](#-112-objectis) 69 | * String Extensions 70 | * [String.startsWith()](#-121-stringstartswith) 71 | * [String.endsWith()](#-122-stringendswith) 72 | * [String.includes()](#-123-stringincludes) 73 | * Proxy & Reflection 74 | * [Proxy](#-131-proxies) 75 | * [Reflection](#-132-reflect) 76 | * Miscellaneous Features 77 | * [Unicode](#-141-unicode) 78 | * [Proper Tail calls](#-142-proper-tail-calls) 79 | 80 |
81 | 82 | ## # 1. Introduction 83 | 84 | JavaScript ES6 (also known as ECMAScript 2015 or ECMAScript 6) is the newer version of JavaScript that was introduced in 2015. 85 | 86 | ECMAScript is the standard that JavaScript programming language uses. ECMAScript provides the specification on how JavaScript programming language should work. 87 | 88 |
89 | ↥ back to top 90 |
91 | 92 | ## # 2.1. let 93 | 94 | ES6 provides a new way of declaring a variable by using the `let` keyword. The `let` keyword is similar to the `var` keyword, except that these variables are **blocked-scope**. 95 | 96 | **Syntax:** 97 | 98 | ```js 99 | let variable_name; 100 | ``` 101 | 102 | In JavaScript, blocks are denoted by curly braces `{}` , for example, the `if else`, `for`, `do while`, `while`, `try catch` and so on. 103 | 104 | **Example:** 105 | 106 | ```js 107 | let x = 10; 108 | if (x === 10) { 109 | let x = 20; 110 | console.log(x); // 20: reference x inside the block 111 | } 112 | console.log(x); // 10: reference at the begining of the script 113 | 114 | // Output: 115 | 20 116 | 10 117 | ``` 118 | 119 | Because the `let` keyword declares a block-scoped variable, the `x` variable inside the `if` block is a **new variable** and it shadows the `x` variable declared at the top of the script. Therefore, the value of `x` in the console is 20. 120 | 121 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-let-zikqqb?file=/src/index.js)** 122 | 123 |
124 | ↥ back to top 125 |
126 | 127 | ## # 2.2. let vs var 128 | 129 | The `var` variables belong to the global scope when you define them outside a function. When you declare a variable inside a function using the `var` keyword, the scope of the variable is local. For example: 130 | 131 | ```js 132 | function increase() { 133 | var counter = 10; 134 | } 135 | console.log(counter); // cannot access the counter variable here 136 | 137 | // Output 138 | // ReferenceError: counter is not defined 139 | ``` 140 | 141 | Here, the counter variable is local to the increase() function. It cannot be accessible outside of the function. 142 | 143 | The following example displays four numbers from 0 to 4 inside the loop and the number 5 outside the loop. 144 | 145 | ```js 146 | for (var i = 0; i < 3; i++) { 147 | console.log("Inside the loop:", i); 148 | } 149 | 150 | console.log("Outside the loop:", i); 151 | 152 | // Output: 153 | Inside the loop: 0 154 | Inside the loop: 1 155 | Inside the loop: 2 156 | Outside the loop: 3 157 | ``` 158 | 159 | Here, the i variable is a global variable. Therefore, it can be accessed from both inside and after the for loop. 160 | 161 | The following example uses the `let` keyword instead of the `var` keyword: 162 | 163 | ```js 164 | for (let i = 0; i < 3; i++) { 165 | console.log("Inside the loop:", i); 166 | } 167 | 168 | console.log("Outside the loop:", i); 169 | 170 | // Output 171 | Inside the loop: 0 172 | Inside the loop: 1 173 | Inside the loop: 2 174 | 175 | Uncaught ReferenceError: i is not defined 176 | ``` 177 | 178 | Here, the variable **i** is blocked scope. It means that the variable **i** only exists and can be accessible inside the for loop block. 179 | 180 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-let-vs-var-yvu11z)** 181 | 182 |
183 | ↥ back to top 184 |
185 | 186 | ## # 2.3. const 187 | 188 | ES6 provides a new way of declaring a constant by using the `const` keyword. The `const` keyword creates a **read-only** reference to a value. 189 | 190 | ```js 191 | const CONSTANT_NAME = value; 192 | ``` 193 | 194 | Like the `let` keyword, the `const` keyword declares **blocked-scope** variables. However, the **block-scoped** variables declared by the `const` keyword can\'t be **reassigned**. 195 | 196 | ```js 197 | const RATE = 0.1; 198 | RATE = 0.2; // TypeError 199 | ``` 200 | 201 | The const keyword ensures that the variable it creates is read-only. However, it doesn’t mean that the actual value to which the const variable reference is immutable. For example: 202 | 203 | ```js 204 | const person = { age: 20 }; 205 | console.log(person.age); // 20 206 | 207 | person.age = 30; // OK 208 | console.log(person.age); // 30 209 | ``` 210 | 211 | Even though the person variable is a constant, you can change the value of its property. 212 | 213 | However, you cannot reassign a different value to the person constant like this: 214 | 215 | ```js 216 | person = { age: 40 }; // TypeError 217 | ``` 218 | 219 | If you want the value of the person object to be immutable, you have to freeze it by using the Object.freeze() method: 220 | 221 | ```js 222 | const person = Object.freeze({age: 20}); 223 | person.age = 30; // TypeError 224 | ``` 225 | 226 | *Note: `Object.freeze()` is shallow, meaning that it can freeze the properties of the object, not the objects referenced by the properties.* 227 | 228 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-const-prej6w)** 229 | 230 |
231 | ↥ back to top 232 |
233 | 234 | ## # 2.4. Arrow Function 235 | 236 | The Arrow function provides a more concise syntax for writing function expressions by opting out the function and return keywords using fat arrow(`=>`) notation. 237 | 238 | **Syntax:** 239 | 240 | ```js 241 | let myFunction = (arg1, arg2, ...argN) => expression 242 | ``` 243 | 244 | ```js 245 | // Function Expression 246 | let add = function (x, y) { 247 | return x + y; 248 | }; 249 | console.log(add(10, 20)); // 30 250 | ``` 251 | 252 | The above function can be written as 253 | 254 | ```js 255 | // Arrow functions 256 | let add = (x, y) => x + y; 257 | console.log(add(10, 20)); // 30 258 | ``` 259 | 260 | **Example 01:** Arrow Function with No Argument 261 | 262 | If a function doesn\'t take any argument, then you should use empty parentheses. 263 | 264 | ```js 265 | let greet = () => console.log('Hello'); 266 | greet(); // Hello 267 | ``` 268 | 269 | **Example 02:** Arrow Function with One Argument 270 | 271 | If a function has only one argument, you can omit the parentheses. 272 | 273 | ```js 274 | let greet = x => console.log(x); 275 | greet('Hello'); // Hello 276 | ``` 277 | 278 | **Example 03:** Arrow Function as an Expression 279 | 280 | You can also dynamically create a function and use it as an expression. 281 | 282 | ```js 283 | let age = 25; 284 | 285 | let welcome = (age < 18) ? 286 | () => console.log('Baby') : 287 | () => console.log('Adult'); 288 | 289 | welcome(); // Adult 290 | ``` 291 | 292 | **Example 04:** Multiline Arrow Functions 293 | 294 | If a function body has multiple statements, you need to put them inside curly brackets `{}`. 295 | 296 | ```js 297 | let area = (r) => { 298 | const pi = 3.14; 299 | return pi * r * r; 300 | } 301 | 302 | let result = area(10); 303 | console.log(result); // 314 304 | ``` 305 | 306 | *Note: Unlike regular functions, arrow functions do not have their own `this`. The value of `this` inside an arrow function remains the same throughout the lifecycle of the function and is always bound to the value of `this` in the closest non-arrow parent function.* 307 | 308 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-arrow-function-yl7oqo?file=/src/index.js)** 309 | 310 |
311 | ↥ back to top 312 |
313 | 314 | ## # 2.5. Default Function Parameters 315 | 316 | Default parameters allow named parameters of a function to be initialized with default values if no value or undefined is passed. 317 | 318 | **Syntax:** 319 | 320 | ```js 321 | function fn(param1=default1, param2=default2,..) { 322 | // ... 323 | } 324 | ``` 325 | 326 | Prior to ES6, you need check for undefined values and provide the default value for undefined values using if/else or ternary operator 327 | 328 | ```js 329 | function sum(a, b) { 330 | a = (typeof a !== 'undefined') ? a : 10; 331 | b = (typeof b !== 'undefined') ? b : 20; 332 | return a + b; 333 | } 334 | 335 | console.log(sum()); // 30 336 | console.log(sum(20)); // 40 337 | ``` 338 | 339 | In ES6, these checks can be avoided using default parameters 340 | 341 | ```js 342 | function sum(a = 10, b = 20) { 343 | return a + b; 344 | } 345 | 346 | console.log(sum()); // 30 347 | console.log(sum(20)); // 40 348 | console.log(sum(20, 30)); // 50 349 | ``` 350 | 351 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-default-parameters-8uu0m8?file=/src/index.js)** 352 | 353 |
354 | ↥ back to top 355 |
356 | 357 | ## # 2.6. Rest Parameter 358 | 359 | The rest parameter is used to represent an indefinite number of arguments as an array. The important point here is only the function\'s last parameter can be a "rest parameter". 360 | 361 | This feature has been introduced to reduce the boilerplate code that was induced by the arguments. 362 | 363 | **Example:** 364 | 365 | ```js 366 | function sum(...args) { 367 | return args.reduce((previous, current) => { 368 | return previous + current; 369 | }); 370 | } 371 | 372 | console.log(sum(10)); // 10 373 | console.log(sum(10, 20)); // 30 374 | console.log(sum(10, 20, 30)); // 60 375 | ``` 376 | 377 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-rest-parameters-w8zy28?file=/src/index.js)** 378 | 379 |
380 | ↥ back to top 381 |
382 | 383 | ## # 2.7. Spread Operator 384 | 385 | The spread operator allows you to spread out elements of an iterable object such as an **array**, **map**, or **set**. 386 | 387 | **Example:** 388 | 389 | ```js 390 | const odd = [1, 3, 5]; 391 | const combined = [2, 4, 6, ...odd]; 392 | 393 | console.log(combined); // [ 2, 4, 6, 1, 3, 5 ] 394 | ``` 395 | 396 | JavaScript spread operator and array manipulation 397 | 398 | **1. Constructing array literal:** 399 | 400 | The spread operator allows to insert another array into the initialized array when you construct an array using the literal form. 401 | 402 | ```js 403 | let initialChars = ['A', 'B']; 404 | let chars = [...initialChars, 'C', 'D']; 405 | 406 | console.log(chars); // ["A", "B", "C", "D"] 407 | ``` 408 | 409 | **2. Concatenating arrays:** 410 | 411 | ```js 412 | let numbers = [10, 20]; 413 | let moreNumbers = [30, 40]; 414 | let allNumbers = [...numbers, ...moreNumbers]; 415 | 416 | console.log(allNumbers); // [10, 20, 30, 40] 417 | ``` 418 | 419 | **3. Copying an array:** 420 | 421 | ```js 422 | let scores = [80, 70, 90]; 423 | let copiedScores = [...scores]; 424 | 425 | console.log(copiedScores); // [80, 70, 90] 426 | ``` 427 | 428 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-spread-operator-0jh98u)** 429 | 430 |
431 | ↥ back to top 432 |
433 | 434 | ## # 2.8. For..of 435 | 436 | The `for...of` statement creates a loop iterating over **iterable objects**, including: built-in `String`, `Array`, array-like objects (e.g., `arguments` or `NodeList`), `TypedArray`, `Map`, `Set`, and user-defined iterables. 437 | 438 | It invokes a custom iteration hook with statements to be executed for the value of each distinct property of the object. 439 | 440 | **Syntax:** 441 | 442 | ```js 443 | for (variable of iterable) { 444 | // ... 445 | } 446 | ``` 447 | 448 | **Example 01:** Iterating over an Array 449 | 450 | ```js 451 | const iterable = [10, 20, 30]; 452 | 453 | for (const value of iterable) { 454 | console.log(value); 455 | } 456 | 457 | // Output 458 | 10 459 | 20 460 | 30 461 | ``` 462 | 463 | **Example 02:** Iterating over a String 464 | 465 | ```js 466 | const iterable = 'Hello'; 467 | 468 | for (const value of iterable) { 469 | console.log(value); 470 | } 471 | 472 | // Output 473 | "H" 474 | "e" 475 | "l" 476 | "l" 477 | "o" 478 | ``` 479 | 480 | **Example 03:** Iterating over a Map 481 | 482 | ```js 483 | const iterable = new Map([['A', 10], ['B', 20], ['C', 30]]); 484 | 485 | for (const [key, value] of iterable) { 486 | console.log(key + " -> " + value); 487 | } 488 | 489 | // Output 490 | A -> 10 491 | B -> 20 492 | C -> 30 493 | ``` 494 | 495 | **Example 04:** Iterating over a Set 496 | 497 | ```js 498 | const iterable = new Set([10, 10, 20, 20, 30, 30]); 499 | 500 | for (const value of iterable) { 501 | console.log(value); 502 | } 503 | 504 | // Output 505 | 10 506 | 20 507 | 30 508 | ``` 509 | 510 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-for-of-ltt89x?file=/src/index.js)** 511 | 512 |
513 | ↥ back to top 514 |
515 | 516 | ## # 2.9. Binary and Octal literals 517 | 518 | ES5 provided numeric literals in octal (prefix 0), decimal (no prefix), and hexadecimal ( 0x) representation. ES6 added support for binary literals and improvements on octal literals. 519 | 520 | **1. Binary literals:** 521 | 522 | Prior to ES5, JavaScript didn\'t provide any literal form of binary numbers. So you need to use a binary string with the help of `parseInt()` 523 | 524 | ```js 525 | const num = parseInt('111', 2); 526 | console.log(num); // 7 527 | ``` 528 | 529 | Whereas ES6 added support for binary literals using the **0b** prefix followed by a sequence of binary numbers (i.e, 0 and 1). 530 | 531 | ```js 532 | const num = 0b111; 533 | console.log(num); // 7 534 | ``` 535 | 536 | **2. Octal literals:** 537 | 538 | In ES5, to represent an octal literal, you use the zero prefix (0) followed by a sequence of octal digits (from 0 to 7). 539 | 540 | ```js 541 | const num = 055; 542 | console.log(num); // 45 543 | 544 | // Note: Legacy octal literals are not allowed in strict mode 545 | ``` 546 | 547 | ES6 represents the octal literal by using the prefix **0o** followed by a sequence of octal digits from 0 through 7. 548 | 549 | ```js 550 | let num = 0o10; 551 | console.log(num); // 8 552 | ``` 553 | 554 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-binary-literals-mllutq?file=/src/index.js)** 555 | 556 |
557 | ↥ back to top 558 |
559 | 560 | ## # 2.10. Template literals 561 | 562 | Template literals allows you to work with strings in a new way compared to ES5. These are just string literals allowing embedded expressions denoted by the dollar sign and curly braces **${expression}**. These literals are enclosed by the backtick character instead of double or single quotes. 563 | 564 | **Example:** 565 | 566 | ```js 567 | let str = "World"; 568 | let message = `Hello ${str}`; 569 | 570 | console.log(message); // Hello World 571 | ``` 572 | 573 | **Multiline Strings:** 574 | 575 | ```js 576 | let msg = 'Multiline \n\ 577 | string'; 578 | 579 | console.log(msg); 580 | 581 | // Multiline 582 | // string 583 | ``` 584 | 585 | **Tagged Templates:** 586 | 587 | A template tag carries the transformation on the template literal and returns the result string. 588 | 589 | It can be used in creating components in `CSS-In-JS` styled components to use across the application 590 | 591 | ```js 592 | function tag(strings) { 593 | console.log(strings.raw[0]); 594 | } 595 | 596 | tag`Hello World`; 597 | 598 | // Output 599 | Hello World 600 | ``` 601 | 602 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-template-literals-d005t4?file=/src/index.js)** 603 | 604 |
605 | ↥ back to top 606 |
607 | 608 | ## # 2.11. Enhanced Object literals 609 | 610 | The object literal is one of the most popular patterns for creating objects in JavaScript. Object literals are extended to support setting the prototype at construction, defining methods, making super calls, and computing property names with expressions 611 | 612 | The important enhancements of object literals are, 613 | 614 | **Property Shorthand:** 615 | 616 | Object\'s properties are often created from variables with the same name. 617 | 618 | Let\'s see the ES5 representation 619 | 620 | ```js 621 | var a = 1, b = 2, c = 3; 622 | var obj = { 623 | a: a, 624 | b: b, 625 | c: c 626 | }; 627 | console.log(obj); 628 | 629 | // Output 630 | {a: 1, b: 2, c: 3} 631 | ``` 632 | 633 | and it can be represented in a shorter syntax as below, 634 | 635 | ```js 636 | let a = 1, b = 2, c = 3; 637 | let obj = { 638 | a, 639 | b, 640 | c 641 | }; 642 | console.log(obj); 643 | 644 | // Output 645 | {a: 1, b: 2, c: 3} 646 | ``` 647 | 648 | **Method Shorthand:** 649 | 650 | Prior to ES6, when defining a method for an object literal, you need to specify the name and full function definition 651 | 652 | ```js 653 | let server = { 654 | name: "Server", 655 | restart: function () { 656 | console.log("The" + this.name + " is restarting..."); 657 | } 658 | }; 659 | server.restart(); 660 | 661 | // Output 662 | The Server is restarting... 663 | ``` 664 | 665 | This can be avoided in ES6, 666 | 667 | ```js 668 | let server = { 669 | name: "Server", 670 | restart() { 671 | console.log("The " + this.name + " is restarting..."); 672 | } 673 | }; 674 | server.restart(); 675 | 676 | // Output 677 | The Server is restarting... 678 | ``` 679 | 680 | **Computed Property Names:** 681 | 682 | In ES5, it wasn\'t possible to use a variable for a key name during object creation stage. 683 | 684 | ```js 685 | var key = "three", 686 | obj = { 687 | one: 10, 688 | two: 20 689 | }; 690 | obj[key] = 30; 691 | ``` 692 | 693 | Object keys can be dynamically assigned in ES6 by placing an expression in square brackets([]) 694 | 695 | ```js 696 | const key = "three", 697 | computedObj = { 698 | one: 10, 699 | two: 20, 700 | [key]: 30 701 | }; 702 | console.log(computedObj.one); 703 | 704 | // Output 705 | 10 706 | ``` 707 | 708 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-object-literals-byphgz?file=/src/index.js)** 709 | 710 |
711 | ↥ back to top 712 |
713 | 714 | ## # 3.1 Array Destructuring 715 | 716 | ES6 provides a new feature called destructing assignment that allows you to destructure properties of an object or elements of an array into individual variables. 717 | 718 | **Basic variable assignment:** 719 | 720 | ```js 721 | const keys = ["one", "two", "three"]; 722 | const [red, yellow, green] = keys; 723 | 724 | console.log(red); // one 725 | console.log(yellow); // two 726 | console.log(green); // three 727 | ``` 728 | 729 | **Assignment separate from declaration:** 730 | 731 | ```js 732 | const [a, b] = [10, 20]; 733 | 734 | console.log(a); // 10 735 | console.log(b); // 20 736 | ``` 737 | 738 | **Array destructuring and Default values:** 739 | 740 | ```js 741 | const [x = 10, y = 20] = [30]; 742 | 743 | console.log(x); // 30 744 | console.log(y); // 20 745 | ``` 746 | 747 | **Swapping variables:** 748 | 749 | ```js 750 | let a = 10, 751 | b = 20; 752 | 753 | [a, b] = [b, a]; 754 | 755 | console.log(a); // 20 756 | console.log(b); // 10 757 | ``` 758 | 759 | **Assigning the rest of an array to a variable:** 760 | 761 | ```js 762 | const [a, ...b] = [1, 2, 3]; 763 | 764 | console.log(a); // 1 765 | console.log(b); // [2, 3] 766 | ``` 767 | 768 | **Functions that return multiple values:** 769 | 770 | ```js 771 | function calculate(a, b) { 772 | return [a + b, a - b, a * b]; 773 | } 774 | 775 | let [sum, difference, multiplication] = calculate(20, 10); 776 | 777 | console.log(sum, difference, multiplication); // 30, 10, 200 778 | ``` 779 | 780 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-array-destructuring-0k5c35?file=/src/index.js)** 781 | 782 |
783 | ↥ back to top 784 |
785 | 786 | ## # 3.2. Object Destructuring 787 | 788 | ES6 introduces the object destructuring syntax that provides an alternative way to assign properties of an object to variables. 789 | 790 | **Syntax:** 791 | 792 | ```js 793 | let { property1: variable1, property2: variable2 } = object; 794 | ``` 795 | 796 | **Simple assignment:** 797 | 798 | ```js 799 | const num = { x: 10, y: 20 }; 800 | const { x, y } = num; 801 | 802 | console.log(x); // 10 803 | console.log(y); // 20 804 | ``` 805 | 806 | **Basic Object destructuring assignment:** 807 | 808 | ```js 809 | const employee = { 810 | eId: "12345", 811 | name: "Dushyant Meena", 812 | email: "dushyant.meena@email.com" 813 | }; 814 | const { eId, name, email } = employee; 815 | 816 | console.log(eId); // 12345 817 | console.log(name); // Dushyant Meena 818 | console.log(email); // dushyant.meena@email.com 819 | ``` 820 | 821 | **Object destructuring and default values:** 822 | 823 | ```js 824 | const { q = 10, w = 20 } = { e: 30 }; 825 | 826 | console.log(q); // 10 827 | console.log(w); // 20 828 | ``` 829 | 830 | **Assigning new variable names:** 831 | 832 | ```js 833 | const number = { x: 10, y: 20 }; 834 | const { x: val1, y: val2 } = number; 835 | 836 | console.log(val1); // 10 837 | console.log(val2); // 20 838 | ``` 839 | 840 | **Assignment without declaration:** 841 | 842 | ```js 843 | let firstName, lastName; 844 | ({ firstName, lastName } = { firstName: "Gauri", lastName: "Pratima" }); 845 | 846 | console.log(firstName); // Gauri 847 | console.log(lastName); // Pratima 848 | ``` 849 | 850 | **Object destructuring and rest operator:** 851 | 852 | ```js 853 | let { a, b, ...args } = { a: 10, b: 20, c: 30, d: 40, e: 50 }; 854 | 855 | console.log(a); 856 | console.log(b); 857 | console.log(args); 858 | 859 | // Output 860 | 10 861 | 20 862 | { c: 30, d: 40, e: 50 } 863 | ``` 864 | 865 | **Assigning new variable names and providing default values simultaneously:** 866 | 867 | ```js 868 | const { a: num1 = 10, b: num2 = 20 } = { a: 30 }; 869 | 870 | console.log(num1); // 30 871 | console.log(num2); // 20 872 | ``` 873 | 874 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-object-destructuring-ecncqm?file=/src/index.js)** 875 | 876 |
877 | ↥ back to top 878 |
879 | 880 | ## # 4. ES6 modules 881 | 882 | An ES6 module is a JavaScript file that executes in strict mode only. It means that any variables or functions declared in the module won\'t be added automatically to the global scope. 883 | 884 | ES6 has provided the built-in support for modules. Everything inside a module is private by default. Public variables, functions and classes are exposed using `export` statement and import the same using `import` statement. 885 | 886 | **Export Statement:** 887 | 888 | There are two types of exports: 889 | 890 | **1. Named Exports:** 891 | 892 | You can export each element or a single export statement to export all the elements at once 893 | 894 | ```js 895 | // module "my-module.js" 896 | const PI = Math.PI; 897 | 898 | function add(...args) { 899 | return args.reduce((num, tot) => tot + num); 900 | } 901 | 902 | function multiply(...args) { 903 | return args.reduce((num, tot) => tot * num); 904 | } 905 | 906 | // private function 907 | function print(msg) { 908 | console.log(msg); 909 | } 910 | 911 | export { PI, add, multiply }; 912 | ``` 913 | 914 | **2. Default Exports:** 915 | 916 | If we want to export a single value, you could use a default export 917 | 918 | ```js 919 | // module "my-module.js" 920 | export default function add(...args) { 921 | return args.reduce((num, tot) => tot + num); 922 | } 923 | ``` 924 | 925 | **Import Statement:** 926 | 927 | The static import statement is used to import read only live bindings which are exported by another module. 928 | 929 | There are many variations of import scenarios as below, 930 | 931 | ```js 932 | // 1. Import an entire module's contents 933 | import * as name from "my-module"; 934 | 935 | //2.Import a single export from a module 936 | import { export1 } from "my-module"; 937 | 938 | //3.Import multiple exports from a module 939 | import { export1 , export2 } from "my-module"; 940 | 941 | //4.Import default export from a module 942 | import defaultExport from "my-module"; 943 | 944 | //5.Import an export with an alias 945 | import { export1 as alias1 } from "my-module"; 946 | ``` 947 | 948 |
949 | ↥ back to top 950 |
951 | 952 | ## # 5.1. Classes 953 | 954 | ES6 Classes formalize the common JavaScript pattern of simulating class-like inheritance hierarchies using functions and prototypes. It support prototype-based inheritance, constructors, super calls, instance and static methods. 955 | 956 | **Example:** 957 | 958 | ```js 959 | /** 960 | * ES6 Class 961 | */ 962 | class Person { 963 | constructor(name) { 964 | this.name = name; 965 | } 966 | getName() { 967 | return this.name; 968 | } 969 | } 970 | 971 | let person = new Person("Prasad Shashi"); 972 | console.log(person.getName()); // "Prasad Shashi" 973 | ``` 974 | 975 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-class-70hkd7)** 976 | 977 |
978 | ↥ back to top 979 |
980 | 981 | ## # 5.2. Getters and Setters 982 | 983 | The accessor properties are methods that get or set the value of an object. For that, we use these two keywords: 984 | 985 | * `get` - to define a getter method to get the property value 986 | * `set` - to define a setter method to set the property value 987 | 988 | **Example:** 989 | 990 | ```js 991 | /** 992 | * Getters and Setters 993 | */ 994 | class Person { 995 | constructor(name) { 996 | this.name = name; 997 | } 998 | get name() { 999 | return this._name; 1000 | } 1001 | set name(name) { 1002 | this._name = name; 1003 | } 1004 | } 1005 | 1006 | let person = new Person("Mala Amar"); 1007 | console.log(person.name); // "Mala Amar" 1008 | ``` 1009 | 1010 | *Note: These setter and getter allow you to use the properties directly ( without using the parenthesis )* 1011 | 1012 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-getters-and-setters-ugb9jx?file=/src/index.js)** 1013 | 1014 |
1015 | ↥ back to top 1016 |
1017 | 1018 | ## # 5.3. Class expressions 1019 | 1020 | A class expression is another way to define a class. Class expressions can be named or unnamed. The name given to a named class expression is local to the class\'s body. However, it can be accessed via the name property. 1021 | 1022 | **Example:** 1023 | 1024 | ```js 1025 | /** 1026 | * Unnamed Class 1027 | */ 1028 | let Person = class { 1029 | constructor(name) { 1030 | this.name = name; 1031 | } 1032 | getName() { 1033 | return this.name; 1034 | } 1035 | }; 1036 | 1037 | let person = new Person("Anjali Durga"); 1038 | console.log(person.getName()); // "Anjali Durga" 1039 | ``` 1040 | 1041 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-class-expressions-ofbg0i)** 1042 | 1043 |
1044 | ↥ back to top 1045 |
1046 | 1047 | ## # 5.4. Static methods 1048 | 1049 | The static keyword defines a static method or property for a class, or a class static initialization block. Neither static methods nor static properties can be called on instances of the class. Instead, they\'re called on the class itself. 1050 | 1051 | Static methods are often utility functions, such as functions to create or clone objects, whereas static properties are useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances. 1052 | 1053 | **Example:** 1054 | 1055 | ```js 1056 | /** 1057 | * Static methods 1058 | */ 1059 | class Person { 1060 | constructor(name) { 1061 | this.name = name; 1062 | } 1063 | 1064 | static staticMethod(gender) { 1065 | let name = gender === "male" ? "Aryan Sarin" : "Manju Soni"; 1066 | return new Person(name); 1067 | } 1068 | } 1069 | 1070 | let anonymous = Person.staticMethod("male"); 1071 | console.log(anonymous); // Person {name: "Aryan Sarin"} 1072 | ``` 1073 | 1074 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-static-methods-3tf3li?file=/src/index.js)** 1075 | 1076 |
1077 | ↥ back to top 1078 |
1079 | 1080 | ## # 5.5. Static Properties 1081 | 1082 | Like a static method, a static property is shared by all instances of a class. To define static property, you use the static keyword followed by the property name like this: 1083 | 1084 | **Example:** 1085 | 1086 | ```js 1087 | /** 1088 | * Static Properties 1089 | */ 1090 | class Item { 1091 | constructor(name, quantity) { 1092 | this.name = name; 1093 | this.quantity = quantity; 1094 | this.constructor.count++; 1095 | } 1096 | // Static Properties 1097 | static count = 0; 1098 | 1099 | // Static Method 1100 | static getCount() { 1101 | return Item.count++; 1102 | } 1103 | } 1104 | 1105 | let pen = new Item("Pen", 5); 1106 | let notebook = new Item("notebook", 10); 1107 | 1108 | console.log(Item.getCount()); // 2 1109 | ``` 1110 | 1111 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-static-properties-xc03d4)** 1112 | 1113 |
1114 | ↥ back to top 1115 |
1116 | 1117 | ## # 5.6. Computed Property 1118 | 1119 | ES6 "Computed Property" feature allows you to have an expression in brackets `[]` (a piece of code that results in a single value like a variable or function invocation) be computed as a property name on an object. 1120 | 1121 | **Example:** 1122 | 1123 | ```js 1124 | /** 1125 | * Computed Property 1126 | */ 1127 | let propName = "fullName"; 1128 | 1129 | class Person { 1130 | constructor(firstName, lastName) { 1131 | this.firstName = firstName; 1132 | this.lastName = lastName; 1133 | } 1134 | get [propName]() { 1135 | return `${this.firstName} ${this.lastName}`; 1136 | } 1137 | } 1138 | 1139 | let person = new Person("Sharma", "Peri"); 1140 | console.log(person.fullName); 1141 | 1142 | // Output 1143 | Sharma Peri 1144 | ``` 1145 | 1146 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-computed-property-ns52qr?file=/src/index.js)** 1147 | 1148 |
1149 | ↥ back to top 1150 |
1151 | 1152 | ## # 5.7. Inheritance 1153 | 1154 | To create a class inheritance, use the `extends` keyword. A class created with a class inheritance inherits all the methods from another class. The `super()` method in the constructor is used to access all parent\'s properties and methods that are used by the derived class. 1155 | 1156 | **Example:** 1157 | 1158 | ```js 1159 | /** 1160 | * Inheritance 1161 | */ 1162 | 1163 | // Parent Class 1164 | class Vehicle { 1165 | constructor(name, type) { 1166 | this.name = name; 1167 | this.type = type; 1168 | } 1169 | 1170 | getName() { 1171 | return this.name; 1172 | } 1173 | 1174 | getType() { 1175 | return this.type; 1176 | } 1177 | } 1178 | // Child Class 1179 | class Car extends Vehicle { 1180 | constructor(name) { 1181 | super(name, "car"); 1182 | } 1183 | 1184 | getName() { 1185 | return "It is a car: " + super.getName(); 1186 | } 1187 | } 1188 | let car = new Car("Tesla"); 1189 | 1190 | console.log(car.getName()); // It is a car: Tesla 1191 | console.log(car.getType()); // car 1192 | ``` 1193 | 1194 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-inheritance-vposow)** 1195 | 1196 |
1197 | ↥ back to top 1198 |
1199 | 1200 | ## # 5.8. new.target 1201 | 1202 | The `new.target` pseudo-property lets you detect whether a function or constructor was called using the **new** operator. In constructors and functions invoked using the new operator, `new.target` returns a reference to the constructor or function. In normal function calls, `new.target` is undefined. 1203 | 1204 | **Example:** 1205 | 1206 | ```js 1207 | /** 1208 | * new.target 1209 | */ 1210 | class Person { 1211 | constructor(name) { 1212 | this.name = name; 1213 | console.log(new.target.name); 1214 | } 1215 | } 1216 | 1217 | class Employee extends Person { 1218 | constructor(name, title) { 1219 | super(name); 1220 | this.title = title; 1221 | } 1222 | } 1223 | 1224 | let person = new Person("Tara Mishra"); // Person 1225 | let employee = new Employee("Aditya Kala", "Programmer"); // Employee 1226 | ``` 1227 | 1228 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-new-target-dzurzb?file=/src/index.js)** 1229 | 1230 |
1231 | ↥ back to top 1232 |
1233 | 1234 | ## # 6. Symbol 1235 | 1236 | The JavaScript ES6 introduced a new primitive data type called Symbol. Symbols are immutable (cannot be changed) and are unique. 1237 | Symbols are often used to add unique property keys to an object that won\'t collide with keys any other code might add to the object 1238 | 1239 | **Creating Symbols:** 1240 | 1241 | **Example 01:** 1242 | 1243 | ```js 1244 | // Two symbols with the same description 1245 | 1246 | const sym1 = Symbol("Hi"); 1247 | const sym2 = Symbol("Hi"); 1248 | 1249 | console.log(sym1 === sym2); // false 1250 | ``` 1251 | 1252 | **Sharing Symbols:** 1253 | 1254 | ES6 provides you with the global symbol registry that allows you to share symbols globally. If you want to create a symbol that will be shared, you use the `Symbol.for()` method instead of calling the `Symbol()` function. 1255 | 1256 | The `Symbol.for()` method accepts a single parameter that can be used for symbol\'s description 1257 | 1258 | **Example 02:** 1259 | 1260 | ```js 1261 | let ssn = Symbol.for("ssn"); 1262 | let citizenID = Symbol.for("ssn"); 1263 | console.log(ssn === citizenID); // true 1264 | console.log(Symbol.keyFor(ssn)); // ssn 1265 | ``` 1266 | 1267 | The `Symbol.for()` method first searches for the symbol with the ssn key in the global symbol registry. It returns the existing symbol if there is one. Otherwise, the `Symbol.for()` method creates a new symbol, registers it to the global symbol registry with the specified key, and returns the symbol. 1268 | 1269 | **Add Symbol as an Object Key:** 1270 | 1271 | You can add symbols as a key in an object using square brackets `[]`. 1272 | 1273 | **Example 03:** 1274 | 1275 | ```js 1276 | let id = Symbol("id"); 1277 | let person = { 1278 | name: "Anjali Mistry", 1279 | // adding symbol as a key 1280 | [id]: 10 // not "id": 10 1281 | }; 1282 | console.log(person); // {name: 'Anjali Mistry', Symbol(id): 10} 1283 | ``` 1284 | 1285 | **Symbol Methods:** 1286 | 1287 | |Method |Description 1288 | |-----------|------------------------| 1289 | |for() |Searches for existing symbols| 1290 | |keyFor() |Returns a shared symbol key from the global symbol registry.| 1291 | |toSource() |Returns a string containing the source of the Symbol object| 1292 | |toString() |Returns a string containing the description of the Symbol| 1293 | |valueOf() |Returns the primitive value of the Symbol object.| 1294 | 1295 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-symbol-xweo3x?file=/src/index.js)** 1296 | 1297 |
1298 | ↥ back to top 1299 |
1300 | 1301 | ## # 7.1. Iterators 1302 | 1303 | Iterator is an object which allows us to access a collection of objects one at a time. ES6 provides built-in iterators for the collection types `String`, `Array`, `Set`, and `Map`. 1304 | 1305 | In JavaScript an iterator is an object which defines a sequence and potentially a return value upon its termination.Specifically, an iterator is any object which implements the Iterator protocol by having a `next()` method that returns an object with two properties: 1306 | 1307 | * `value`: The next value in the iteration sequence. 1308 | * `done`: This is true if the last value in the sequence has already been consumed. If value is present alongside done, it is the iterator's return value. 1309 | 1310 | **Example:** 1311 | 1312 | The following code creates a Sequence object that returns a list of numbers in the range of ( start, end) with an interval between subsequent numbers. 1313 | 1314 | ```js 1315 | /** 1316 | * Iterators 1317 | */ 1318 | class Sequence { 1319 | constructor(start = 0, end = Infinity, interval = 1) { 1320 | this.start = start; 1321 | this.end = end; 1322 | this.interval = interval; 1323 | } 1324 | [Symbol.iterator]() { 1325 | let counter = 0; 1326 | let nextIndex = this.start; 1327 | return { 1328 | next: () => { 1329 | if (nextIndex <= this.end) { 1330 | let result = { value: nextIndex, done: false }; 1331 | nextIndex += this.interval; 1332 | counter++; 1333 | return result; 1334 | } 1335 | return { value: counter, done: true }; 1336 | } 1337 | }; 1338 | } 1339 | } 1340 | ``` 1341 | 1342 | The following code uses the Sequence iterator in a `for...of` loop: 1343 | 1344 | ```js 1345 | let evenNumbers = new Sequence(2, 10, 2); 1346 | for (const num of evenNumbers) { 1347 | console.log(num); 1348 | } 1349 | 1350 | // Output 1351 | 2 1352 | 4 1353 | 6 1354 | 8 1355 | 10 1356 | ``` 1357 | 1358 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-iterators-jx1led?file=/src/index.js)** 1359 | 1360 |
1361 | ↥ back to top 1362 |
1363 | 1364 | ## # 7.2. Generators 1365 | 1366 | A generator is a function that can stop or suspend midway and then continue from where it stopped while maintaining the context(saved across re-entrances). It can be defined using a function keyword followed by an asterisk (`function* ()`). 1367 | 1368 | This function returns an iterator object and this iterator\'s **next()** method returns an object with a value property containing the yielded value and a done property which indicates whether the generator has yielded its last value. 1369 | 1370 | ```js 1371 | /** 1372 | * Generators 1373 | */ 1374 | function* myGenerator(i) { 1375 | yield i + 10; 1376 | return i + 20; 1377 | } 1378 | const myGenObj = myGenerator(10); 1379 | 1380 | console.log(myGenObj.next().value); // 20 1381 | console.log(myGenObj.next().value); // 30 1382 | console.log(myGenObj.next().value); // undefined 1383 | ``` 1384 | 1385 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/compassionate-lederberg-dxi5kw?file=/src/index.js)** 1386 | 1387 |
1388 | ↥ back to top 1389 |
1390 | 1391 | ## # 7.3. Yield 1392 | 1393 | The `yield` keyword allows you to pause and resume a generator function (`function*`). 1394 | 1395 | The `yield` keyword causes the call to the generator\'s `next()` method to return an `IteratorResult` object with two properties: `value` and `done`. The `value` property is the result of evaluating the `yield` expression, and `done` is `false`, indicating that the generator function has not fully completed. 1396 | 1397 | ```js 1398 | /** 1399 | * Yield 1400 | */ 1401 | function* counter() { 1402 | yield 1; 1403 | yield 2; 1404 | yield 3; 1405 | } 1406 | let count = counter(); 1407 | 1408 | console.log(count.next()); // {value: 1, done: false} 1409 | console.log(count.next()); // {value: 1, done: false} 1410 | console.log(count.next()); // {value: 1, done: false} 1411 | ``` 1412 | 1413 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-yield-zo5i9j?file=/src/index.js)** 1414 | 1415 |
1416 | ↥ back to top 1417 |
1418 | 1419 | ## # 8.1. Promises 1420 | 1421 | A promise is an object which represent the eventual completion or failure of an **asynchronous** operation. 1422 | 1423 | It is in one of these states: 1424 | 1425 | * **pending:** Represents initial state, neither fulfilled nor rejected. 1426 | * **fulfilled:** Indicates that the operation is completed successfully. 1427 | * **rejected:** Indicates that the operation is failed. 1428 | 1429 | In the beginning, the state of a promise is pending, indicating that the asynchronous operation is in progress. Depending on the result of the asynchronous operation, the state changes to either fulfilled or rejected. 1430 | 1431 | The fulfilled state indicates that the asynchronous operation was completed successfully: 1432 | 1433 |

1434 | Promise fulfilled 1435 |

1436 | 1437 | The rejected state indicates that the asynchronous operation failed. 1438 | 1439 |

1440 | Promise rejected 1441 |

1442 | 1443 | **Creating a Promise:** 1444 | 1445 | The promise constructor accepts a callback function that typically performs an asynchronous operation. This function is often referred to as an executor. 1446 | 1447 | In turn, the executor accepts two callback functions with the name `resolve` and `reject`. 1448 | 1449 | ```js 1450 | /** 1451 | * Promise 1452 | */ 1453 | const promise = new Promise((resolve, reject) => { 1454 | // contain an operation 1455 | // ... 1456 | 1457 | // return the state 1458 | if (success) { 1459 | resolve(value); 1460 | } else { 1461 | reject(error); 1462 | } 1463 | }); 1464 | ``` 1465 | 1466 | If the asynchronous operation completes successfully, the executor will call the `resolve()` function to change the state of the promise from pending to fulfilled with a value. 1467 | 1468 | In case of an error, the executor will call `reject()` function to change the state of the promise from pending to rejected with the error reason. 1469 | 1470 | **1. The then() method:** 1471 | 1472 | The `then()` method accepts two callback functions: `onFulfilled` and `onRejected`. 1473 | 1474 | The `then()` method calls the `onFulfilled()` with a value, if the promise is fulfilled or the `onRejected()` with an error if the promise is rejected. 1475 | 1476 | ```js 1477 | promise.then(onFulfilled,onRejected); 1478 | ``` 1479 | 1480 | **2. The catch() method:** 1481 | 1482 | If you want to get the error only when the state of the promise is rejected, you can use the `catch()` method of the Promise object. Internally, the `catch()` method invokes the `.then(undefined, onRejected)` method. 1483 | 1484 | ```js 1485 | promise.catch(onRejected); 1486 | ``` 1487 | 1488 | **3. The finally() method:** 1489 | 1490 | The `.finally()` method returns a Promise. When the promise is finally either fulfilled or rejected, the specified callback function is executed. This provides a way for code to be run whether the promise was fulfilled successfully, or instead rejected. 1491 | 1492 | This helps to avoid duplicating code in both the promise's `.then()` and `.catch()` handlers. 1493 | 1494 | ```js 1495 | const render = () => { 1496 | //... 1497 | }; 1498 | 1499 | getUsers() 1500 | .then((users) => { 1501 | console.log(users); 1502 | render(); // Duplicate Method 1503 | }) 1504 | .catch((error) => { 1505 | console.log(error); 1506 | render(); // Duplicate Method 1507 | }); 1508 | ``` 1509 | 1510 | This can be avoided by using `.finally()` 1511 | 1512 | ```js 1513 | const render = () => { 1514 | //... 1515 | }; 1516 | 1517 | getUsers() 1518 | .then((users) => { 1519 | console.log(users); 1520 | }) 1521 | .catch((error) => { 1522 | console.log(error); 1523 | }) 1524 | .finally(() => { 1525 | render(); 1526 | }); 1527 | ``` 1528 | 1529 | **Example:** 1530 | 1531 | ```js 1532 | let promise = new Promise((resolve, reject) => { 1533 | setTimeout(() => { 1534 | console.log("Promise started..."); 1535 | resolve("Promise resolved"); 1536 | }, 300); 1537 | }) 1538 | .then((value) => { 1539 | console.log("OK: " + value); 1540 | }) 1541 | .catch((value) => { 1542 | console.log("ERROR: " + value); 1543 | }) 1544 | .finally(() => { 1545 | console.log("Final Block"); 1546 | }); 1547 | ``` 1548 | 1549 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-promise-pqn0xf?file=/src/index.js)** 1550 | 1551 |
1552 | ↥ back to top 1553 |
1554 | 1555 | ## # 8.2. Promise Chaining 1556 | 1557 | A common need is to execute two or more asynchronous operations back to back, where each subsequent operation starts when the previous operation succeeds, with the result from the previous step. We accomplish this by creating a **promise chain**. 1558 | 1559 | **Example:** 1560 | 1561 | ```js 1562 | /** 1563 | * Promise Chaining 1564 | */ 1565 | new Promise(function(resolve, reject) { 1566 | 1567 | setTimeout(() => resolve(1), 1000); // (*) 1568 | 1569 | }).then(function(result) { // (**) 1570 | 1571 | alert(result); // 1 1572 | return result * 2; 1573 | 1574 | }).then(function(result) { // (***) 1575 | 1576 | alert(result); // 2 1577 | return result * 2; 1578 | 1579 | }).then(function(result) { 1580 | 1581 | alert(result); // 4 1582 | return result * 2; 1583 | 1584 | }); 1585 | ``` 1586 | 1587 | The idea is that the result is passed through the chain of .then handlers. 1588 | 1589 | Here the flow is: 1590 | 1591 | 1. The initial promise resolves in 1 second (*), 1592 | 1. Then the .then handler is called (**), which in turn creates a new promise (resolved with 2 value). 1593 | 1. The next then (***) gets the result of the previous one, processes it (doubles) and passes it to the next handler. 1594 | 1. …and so on. 1595 | 1596 | As the result is passed along the chain of handlers, we can see a sequence of alert calls: `1 → 2 → 4`. 1597 | 1598 |

1599 | Promise Chaining 1600 |

1601 | 1602 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-promise-chaining-zmxbb5?file=/src/index.js)** 1603 | 1604 |
1605 | ↥ back to top 1606 |
1607 | 1608 | ## # 8.3. Promise.all() 1609 | 1610 | The `Promise.all()` method takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises. This returned promise will resolve when all of the input\'s promises have resolved, or if the input iterable contains no promises. 1611 | 1612 | It rejects immediately upon any of the input promises rejecting or non-promises throwing an error, and will reject with this first rejection message / error. 1613 | 1614 | **Syntax:** 1615 | 1616 | ```js 1617 | Promise.all(iterable); 1618 | ``` 1619 | 1620 | **Example:** 1621 | 1622 | ```js 1623 | /** 1624 | * Promise.all() 1625 | */ 1626 | const p1 = new Promise((resolve, reject) => { 1627 | setTimeout(() => { 1628 | console.log("The first promise has resolved"); 1629 | resolve(10); 1630 | }, 1 * 1000); 1631 | }); 1632 | 1633 | const p2 = new Promise((resolve, reject) => { 1634 | setTimeout(() => { 1635 | console.log("The second promise has resolved"); 1636 | resolve(20); 1637 | }, 2 * 1000); 1638 | }); 1639 | 1640 | const p3 = new Promise((resolve, reject) => { 1641 | setTimeout(() => { 1642 | console.log("The third promise has resolved"); 1643 | resolve(30); 1644 | }, 3 * 1000); 1645 | }); 1646 | 1647 | Promise.all([p1, p2, p3]).then((results) => { 1648 | const total = results.reduce((p, c) => p + c); 1649 | 1650 | console.log(`Results: ${results}`); 1651 | console.log(`Total: ${total}`); 1652 | }); 1653 | ``` 1654 | 1655 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-promise-all-e34qsj?file=/src/index.js)** 1656 | 1657 |
1658 | ↥ back to top 1659 |
1660 | 1661 | ## # 8.4. Promise.race() 1662 | 1663 | The `Promise.race()` static method accepts a list of promises as an iterable object and returns a new promise that fulfills or rejects as soon as there is one promise that fulfills or rejects, with the value or reason from that promise. 1664 | 1665 | **Syntax:** 1666 | 1667 | ```js 1668 | Promise.race(iterable) 1669 | ``` 1670 | 1671 | **Example:** 1672 | 1673 | ```js 1674 | /** 1675 | * Promise.race() 1676 | */ 1677 | const p1 = new Promise((resolve, reject) => { 1678 | setTimeout(() => { 1679 | console.log("The first promise has resolved"); 1680 | resolve(10); 1681 | }, 1 * 1000); 1682 | }); 1683 | 1684 | const p2 = new Promise((resolve, reject) => { 1685 | setTimeout(() => { 1686 | console.log("The second promise has resolved"); 1687 | resolve(20); 1688 | }, 2 * 1000); 1689 | }); 1690 | 1691 | Promise.race([p1, p2]) 1692 | .then((value) => console.log(`Resolved: ${value}`)) 1693 | .catch((reason) => console.log(`Rejected: ${reason}`)); 1694 | ``` 1695 | 1696 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-promise-all-e34qsj?file=/src/index.js)** 1697 | 1698 |
1699 | ↥ back to top 1700 |
1701 | 1702 | ## # 8.5. Promise Error Handling 1703 | 1704 | The `.catch()` method returns a Promise and deals with rejected cases only. It internally calls `obj.then(undefined, onRejected))`. 1705 | 1706 | Inside the promise, the `.catch()` method will catch the error caused by the `throw` statement and `reject()`. If an error occurs and you don\'t have the `.catch()` method, the JavaScript engine issues a runtime error and stops the program. 1707 | 1708 | **Example:** 1709 | 1710 | ```js 1711 | const promise = new Promise((resolve, reject) => { 1712 | throw "An Error Occured!"; 1713 | }).catch((error) => { 1714 | console.error(error); 1715 | }); 1716 | 1717 | // Output: 1718 | An Error Occured! 1719 | ``` 1720 | 1721 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-promise-error-handling-leq6gc?file=/src/index.js)** 1722 | 1723 |
1724 | ↥ back to top 1725 |
1726 | 1727 | ## # 9.1. Set 1728 | 1729 | ES6 provides a new type named `Set` that stores a collection of **unique** values of any type. 1730 | 1731 | The `Set` constructor also accepts an optional **iterable object**. If you pass an iterable object to the `Set` constructor, all the elements of the iterable object will be added to the new set: 1732 | 1733 | **Syntax:** 1734 | 1735 | ```js 1736 | let setObject = new Set(iterableObject); 1737 | ``` 1738 | 1739 | **Set Methods:** 1740 | 1741 | The Set object provides the following useful methods: 1742 | 1743 | |Sl.No.| Methods |Description | 1744 | |------|-------------|-----------------------------------------| 1745 | | 01. |add(value) |appends a new element with a specified value to the set. It returns the Set object, therefore, you can chain this method with another Set method.| 1746 | | 02. |clear() |removes all elements from the Set object.| 1747 | | 03. |delete(value) | deletes an element specified by the value.| 1748 | | 04. |entries() | returns a new Iterator that contains an array of [value, value] .| 1749 | | 05. |forEach(callback [, thisArg]) | invokes a callback on each element of the Set with the this value sets to thisArg in each call.| 1750 | | 06. |has(value) | returns true if an element with a given value is in the set, or false if it is not.| 1751 | | 07. |keys() | is the same as values() function.| 1752 | | 08. |[@@iterator] | returns a new Iterator object that contains values of all elements stored in the insertion order.| 1753 | 1754 | **Example 01:** Create a new Set from an Array 1755 | 1756 | ```js 1757 | let numbers = new Set([10, 20, 20, 30, 40, 50]); 1758 | 1759 | console.log(numbers); // Set(5) {10, 20, 30, 40, 50} 1760 | console.log(typeof numbers); // Object 1761 | ``` 1762 | 1763 | **Example 02:** Get the size of a Set 1764 | 1765 | ```js 1766 | let size = numbers.size; 1767 | 1768 | console.log(size); // 5 1769 | ``` 1770 | 1771 | **Example 03:** Add elements to a Set 1772 | 1773 | ```js 1774 | numbers.add(60); 1775 | 1776 | console.log(numbers); // Set(6) {10, 20, 30, 40, 50, 60} 1777 | ``` 1778 | 1779 | **Example 04:** Check if a value is in the Set 1780 | 1781 | ```js 1782 | let isTrue = numbers.has(10); 1783 | 1784 | console.log(isTrue); // true 1785 | ``` 1786 | 1787 | **Example 05:** Remove elements from a set 1788 | 1789 | ```js 1790 | numbers.delete(60); 1791 | 1792 | console.log(numbers); // Set(5) {10, 20, 30, 40, 50} 1793 | ``` 1794 | 1795 | **Example 06:** Looping the elements of a Set 1796 | 1797 | ```js 1798 | for (let number of numbers) { 1799 | console.log(number); 1800 | } 1801 | 1802 | // Output 1803 | 10 1804 | 20 1805 | 30 1806 | 40 1807 | 50 1808 | ``` 1809 | 1810 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-set-dho0q8?file=/src/index.js)** 1811 | 1812 |
1813 | ↥ back to top 1814 |
1815 | 1816 | ## # 9.2. Weakset 1817 | 1818 | The `WeakSet` store a collection of objects. It adapts the same properties of that of a `set` i.e. does not store duplicates. However, `WeakSet` can only contain **objects** whereas a `Set` can contain any data types such as strings, numbers, objects, etc. Since objects in a `WeakSet` may be automatically **garbage-collected**, a `WeakSet` does not have size property. 1819 | 1820 | **Example:** WeakSet Methods 1821 | 1822 | WeakSets have methods `add()`, `delete()`, and `has()`. 1823 | 1824 | ```js 1825 | /** 1826 | * Weakset 1827 | */ 1828 | const weakSet = new WeakSet(); 1829 | 1830 | let obj = { 1831 | message: 'Hi', 1832 | sendMessage: true 1833 | } 1834 | 1835 | // adding object (element) to WeakSet 1836 | weakSet.add(obj); 1837 | 1838 | // check if an element is in Set 1839 | console.log(weakSet.has(obj)); // true 1840 | 1841 | console.log(weakSet); // WeakSet {{message: "Hi", sendMessage: true}} 1842 | 1843 | // delete elements 1844 | weakSet.delete(obj); 1845 | console.log(weakSet); // WeakSet {} 1846 | ``` 1847 | 1848 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-weakset-cg86e3?file=/src/index.js)** 1849 | 1850 |
1851 | ↥ back to top 1852 |
1853 | 1854 | ## # 9.3. Map 1855 | 1856 | A `Map` object holds **key-value** pairs where values of any type can be used as either `keys` or `values`. In addition, a `Map` object remembers the original insertion order of the `keys`. 1857 | 1858 | **Syntax:** 1859 | 1860 | ```js 1861 | let map = new Map([iterable]); 1862 | ``` 1863 | 1864 | **Map methods:** 1865 | 1866 | |Sl.No.| Methods | Description | 1867 | |------|------------|--------------------------| 1868 | | 01. |clear() | removes all elements from the map object.| 1869 | | 02. |delete(key) | removes an element specified by the key. It returns if the element is in the map, or false if it does not.| 1870 | | 03. |entries() |returns a new Iterator object that contains an array of [key, value] for each element in the map object. The order of objects in the map is the same as the insertion order.| 1871 | | 04. |forEach(callback[, thisArg]) | invokes a callback for each key-value pair in the map in the insertion order. The optional thisArg parameter sets the this value for each callback.| 1872 | | 05. |get(key) | returns the value associated with the key. If the key does not exist, it returns undefined.| 1873 | | 06. |has(key) | returns true if a value associated with the key exists, otherwise, return false.| 1874 | | 07. |keys() |returns a new Iterator that contains the keys for elements in insertion order.| 1875 | | 08. |set(key, value) | sets the value for the key in the map object. It returns the map object itself therefore you can chain this method with other methods.| 1876 | | 09. |values() |returns a new iterator object that contains values for each element in insertion order.| 1877 | 1878 | **Initialize a map with an iterable object:** 1879 | 1880 | ```js 1881 | let lalit = { name: 'Lalit Ranganathan' }, 1882 | jayesh = { name: 'Jayesh Ray' }, 1883 | sarvesh = { name: 'Sarvesh Tripathi' }; 1884 | 1885 | let userRoles = new Map([ 1886 | [lalit, 'admin'], 1887 | [jayesh, 'editor'], 1888 | [sarvesh, 'subscriber'], 1889 | ]); 1890 | 1891 | // Get an element from a map by key 1892 | userRoles.get(john); // admin 1893 | 1894 | // Check the existence of an element by key 1895 | userRoles.has(lily); // true 1896 | ``` 1897 | 1898 | **Iterate over map keys:** 1899 | 1900 | ```js 1901 | /** 1902 | * Map 1903 | */ 1904 | let lalit = { name: 'Lalit Ranganathan' }, 1905 | jayesh = { name: 'Jayesh Ray' }, 1906 | sarvesh = { name: 'Sarvesh Tripathi' }; 1907 | 1908 | let userRoles = new Map([ 1909 | [lalit, 'admin'], 1910 | [jayesh, 'editor'], 1911 | [sarvesh, 'subscriber'], 1912 | ]); 1913 | 1914 | for (const user of userRoles.keys()) { 1915 | console.log(user.name); 1916 | } 1917 | ``` 1918 | 1919 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-map-w6i921?file=/src/index.js)** 1920 | 1921 |
1922 | ↥ back to top 1923 |
1924 | 1925 | ## # 9.4. Weakmap 1926 | 1927 | A `WeakMap` is similar to a `Map` except the `keys` of a `WeakMap` must be **objects**. It means that when a reference to a key (an object) is out of scope, the corresponding value is automatically released from the memory. 1928 | 1929 | A `WeakMap` only has subset methods of a `Map` object: 1930 | 1931 | * get(key) 1932 | * set(key, value) 1933 | * has(key) 1934 | * delete(key) 1935 | 1936 | ```js 1937 | /** 1938 | * Weakmap 1939 | */ 1940 | var weakMap = new WeakMap(); 1941 | var obj1 = {}; 1942 | var obj2 = {}; 1943 | 1944 | weakMap.set(obj1, 10); 1945 | weakMap.set(obj2, 20); 1946 | weakMap.set({}, { four: 4 }); 1947 | 1948 | console.log(weakMap.get(obj2)); // 20 1949 | 1950 | /** 1951 | * Return false even though empty object exists as key. 1952 | * Because the keys have different references 1953 | * */ 1954 | console.log(weakMap.has({})); 1955 | 1956 | weakMap.delete(obj1); 1957 | console.log(weakMap.get(obj1)); //undefined 1958 | ``` 1959 | 1960 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-weakmap-mexzxg?file=/src/index.js)** 1961 | 1962 |
1963 | ↥ back to top 1964 |
1965 | 1966 | ## # 10.1. Array.of() 1967 | 1968 | The `Array.of()` method creates a new Array instance from a variable number of arguments, regardless of number or type of the arguments. 1969 | 1970 | The difference between `Array.of()` and the `Array` constructor is in the handling of integer arguments: Array.of(3) creates an array with a single element, 3, whereas Array(3) creates an empty array with a length property of 3 (Note: this implies an array of 3 empty slots, not slots with actual `undefined` values). 1971 | 1972 | **Syntax:** 1973 | 1974 | ```js 1975 | Array.of(element0, element1, /* ... ,*/ elementN) 1976 | ``` 1977 | 1978 | **Example:** 1979 | 1980 | ```js 1981 | /** 1982 | * Array.of() 1983 | */ 1984 | 1985 | // Array Method 1986 | let number = Array(3); 1987 | console.log(number.length); // 3 1988 | console.log(number[0]); // undefined 1989 | console.log("Array of Method"); 1990 | 1991 | // Array of() Method 1992 | let numbers = Array.of(3); 1993 | console.log(numbers.length); // 1 1994 | console.log(numbers[0]); // 3 1995 | ``` 1996 | 1997 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-array-of-7xgzm6?file=/src/index.js)** 1998 | 1999 |
2000 | ↥ back to top 2001 |
2002 | 2003 | ## # 10.2. Array.from() 2004 | 2005 | The `Array.from()` static method creates a new, shallow-copied `Array` instance from an array-like or iterable object. 2006 | 2007 | **Syntax:** 2008 | 2009 | ```js 2010 | Array.from(target [, mapFn[, thisArg]]) 2011 | ``` 2012 | 2013 | **Create an array from an array-like object:** 2014 | 2015 | ```js 2016 | console.log(Array.from('Hello')); 2017 | 2018 | // Output 2019 | ["H", "e", "l", "l", "o"] 2020 | ``` 2021 | 2022 | **Array.from() with a mapping function:** 2023 | 2024 | ```js 2025 | function addOne() { 2026 | return Array.from(arguments, (x) => x + x); 2027 | } 2028 | console.log(addOne(10, 20, 30)); 2029 | 2030 | // Output 2031 | [20, 40, 60] 2032 | ``` 2033 | 2034 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-array-from-qxefm7?file=/src/index.js)** 2035 | 2036 |
2037 | ↥ back to top 2038 |
2039 | 2040 | ## # 10.3. Array.find() 2041 | 2042 | The `find()` method returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, `undefined` is returned. 2043 | 2044 | **Syntax:** 2045 | 2046 | ```js 2047 | find(callback(element[, index[, array]])[, thisArg]) 2048 | ``` 2049 | 2050 | **Example:** 2051 | 2052 | ```js 2053 | let arr = [2, 4, 5, 6, 7, 10]; 2054 | 2055 | function findFirstOdd(i) { 2056 | return i % 2 !== 0; 2057 | } 2058 | console.log(arr.find(findFirstOdd)); // 5 2059 | ``` 2060 | 2061 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-array-find-iou6gi?file=/src/index.js)** 2062 | 2063 |
2064 | ↥ back to top 2065 |
2066 | 2067 | ## # 10.4. Array.findIndex() 2068 | 2069 | The `findIndex()` method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns `-1`, indicating that no element passed the test. 2070 | 2071 | **Syntax:** 2072 | 2073 | ```js 2074 | findIndex(testFn(element[, index[, array]])[, thisArg]) 2075 | ``` 2076 | 2077 | **Example:** 2078 | 2079 | ```js 2080 | let ranks = [1, 5, 7, 8, 10, 7]; 2081 | 2082 | let index = ranks.findIndex((rank) => rank === 7); 2083 | 2084 | console.log(index); // 2 2085 | ``` 2086 | 2087 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-array-findindex-meiqpf?file=/src/index.js)** 2088 | 2089 |
2090 | ↥ back to top 2091 |
2092 | 2093 | ## # 11.1. Object.assign() 2094 | 2095 | The `Object.assign()` method copies **all enumerable own properties** from one or more source objects to a target object. It returns the modified target object. 2096 | 2097 | The `Object.assign()` invokes the getters on the source objects and setters on the target. It assigns properties only, not copying or defining new properties. 2098 | 2099 | **Example:** Object.assign() to clone an object 2100 | 2101 | ```js 2102 | let widget = { 2103 | color: "red" 2104 | }; 2105 | let clonedWidget = Object.assign({}, widget); 2106 | 2107 | console.log(clonedWidget); 2108 | 2109 | // Output 2110 | { color: 'red' } 2111 | ``` 2112 | 2113 | **Example:** Object.assign() to merge objects 2114 | 2115 | ```js 2116 | let box = { 2117 | height: 10, 2118 | width: 20 2119 | }; 2120 | 2121 | let style = { 2122 | color: "Red", 2123 | borderStyle: "solid" 2124 | }; 2125 | let styleBox = Object.assign({}, box, style); 2126 | 2127 | console.log(styleBox); 2128 | 2129 | // Output 2130 | { 2131 | height: 10, 2132 | width: 20, 2133 | color: "Red", 2134 | borderStyle: "solid" 2135 | } 2136 | ``` 2137 | 2138 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-object-assign-0ot7bs?file=/src/index.js)** 2139 | 2140 |
2141 | ↥ back to top 2142 |
2143 | 2144 | ## # 11.2. Object.is() 2145 | 2146 | The `Object.is()` method determines whether two values are **the same value**. 2147 | 2148 | The `Object.is()` behaves like the `===` operator with two differences: 2149 | 2150 | * -0 and +0 2151 | * NaN 2152 | 2153 | **Example:** Evaluation result is the same as using `===` 2154 | 2155 | ```js 2156 | Object.is(null, null); // true 2157 | Object.is(undefined, undefined); // true 2158 | Object.is(window, window); // true 2159 | Object.is([], []); // false 2160 | 2161 | var obj1 = { a: 1 }; 2162 | var obj2 = { a: 1 }; 2163 | Object.is(obj1, obj1); // true 2164 | Object.is(obj1, obj2); // false 2165 | ``` 2166 | 2167 | **Example:** Negative zero 2168 | 2169 | The === operator treats -0 and +0 are the same value: 2170 | 2171 | ```js 2172 | let amount = +0, 2173 | volume = -0; 2174 | console.log(volume === amount); // true 2175 | 2176 | 2177 | let amount = +0, 2178 | volume = -0; 2179 | console.log(Object.is(amount, volume)); // false 2180 | ``` 2181 | 2182 | **Example:** NaN 2183 | 2184 | The `===` operator considers `NaN` and `NaN` are different values. The `NaN` is the only number that does not equal itself. 2185 | 2186 | ```js 2187 | console.log(Object.is(NaN, 0 / 0)); // true 2188 | console.log(Object.is(NaN, Number.NaN)); // true 2189 | ``` 2190 | 2191 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-object-is-ck2h8y?file=/src/index.js)** 2192 | 2193 |
2194 | ↥ back to top 2195 |
2196 | 2197 | ## # 12.1. String.startsWith() 2198 | 2199 | The `startsWith()` method determines whether a string begins with the characters of a specified string, returning `true` or `false` as appropriate. 2200 | 2201 | **Syntax:** 2202 | 2203 | ```js 2204 | String.startsWith(searchString [,position]) 2205 | ``` 2206 | 2207 | **Example:** 2208 | 2209 | ```js 2210 | let text = "Hello World, Welcome to the JavaScript ES6."; 2211 | let isTrue = text.startsWith("Hello"); 2212 | 2213 | console.log(isTrue); // true 2214 | ``` 2215 | 2216 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-string-startswith-wxbrtw?file=/src/index.js)** 2217 | 2218 |
2219 | ↥ back to top 2220 |
2221 | 2222 | ## 12.2. String.endsWith() 2223 | 2224 | The `endsWith()` method determines whether a string ends with the characters of a specified string, returning `true` or `false` as appropriate. 2225 | 2226 | **Syntax:** 2227 | 2228 | ```js 2229 | String.endsWith(searchString [,length]) 2230 | ``` 2231 | 2232 | **Example:** 2233 | 2234 | ```js 2235 | let text = "Hello World, Welcome to the JavaScript ES6"; 2236 | let isTrue = text.endsWith("ES6"); 2237 | 2238 | console.log(isTrue); // true 2239 | ``` 2240 | 2241 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-string-endswith-1huuuu?file=/src/index.js)** 2242 | 2243 |
2244 | ↥ back to top 2245 |
2246 | 2247 | ## 12.3. String.includes() 2248 | 2249 | The `includes()` method determines whether a string contains another string: 2250 | 2251 | **Syntax:** 2252 | 2253 | ```js 2254 | string.includes(searchString [,position]) 2255 | ``` 2256 | 2257 | **Example:** 2258 | 2259 | ```js 2260 | let email = 'admin@example.com'; 2261 | console.log(email.includes('@')); // true 2262 | 2263 | let str = 'JavaScript String'; 2264 | console.log(str.includes('Script')); // true 2265 | ``` 2266 | 2267 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-includes-ycwhqd?file=/src/index.js)** 2268 | 2269 |
2270 | ↥ back to top 2271 |
2272 | 2273 | ## # 13.1. Proxies 2274 | 2275 | The `Proxy` object allows you to create an object that can be used in place of the original object, but which may redefine fundamental `Object` operations like getting, setting, and defining properties. Proxy objects are commonly used to log property accesses, validate, format, or sanitize inputs, and so on. 2276 | 2277 | The proxy object is created with two parameters: 2278 | 2279 | ```js 2280 | let proxy = new Proxy(target, handler) 2281 | ``` 2282 | 2283 | * **target:** the original object which you want to proxy 2284 | * **handler:** an object that defines which operations will be intercepted and how to redefine intercepted operations. 2285 | 2286 | **Example:** 2287 | 2288 | ```js 2289 | /** 2290 | * Proxy object 2291 | */ 2292 | const user = { 2293 | name: "Rishima Karpe", 2294 | email: "rishima.karpe@email.com" 2295 | }; 2296 | 2297 | const handler = { 2298 | get(target, property) { 2299 | console.log(`Property ${property}:`); 2300 | return target[property]; 2301 | } 2302 | }; 2303 | 2304 | const proxyUser = new Proxy(user, handler); 2305 | console.log(proxyUser.name); 2306 | console.log(proxyUser.email); 2307 | 2308 | // Output 2309 | Property name: Rishima Karpe 2310 | Property email: rishima.karpe@email.com 2311 | ``` 2312 | 2313 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-proxies-u9tgie?file=/src/index.js)** 2314 | 2315 |
2316 | ↥ back to top 2317 |
2318 | 2319 | ## # 13.2. Reflect 2320 | 2321 | Reflection is the ability of a code to inspect and manipulate variables, properties, and methods of objects at **runtime**. 2322 | 2323 | JavaScript already provides `Object.keys()`, `Object.getOwnPropertyDescriptor()`, and `Array.isArray()` methods as classic refection features. 2324 | 2325 | ES6 introduces a new global object called `Reflect` that allows you to call methods, construct objects, get and set properties, manipulate and extend properties. 2326 | 2327 | **Example:** Creating objects using `Reflect.construct()` 2328 | 2329 | ```js 2330 | /** 2331 | * Reflect 2332 | */ 2333 | class User { 2334 | constructor(firstName, lastName) { 2335 | this.firstName = firstName; 2336 | this.lastName = lastName; 2337 | } 2338 | get fullName() { 2339 | return `${this.firstName} ${this.lastName}`; 2340 | } 2341 | } 2342 | let args = ["Abhilash", "Bhasin"]; 2343 | let abhilash = Reflect.construct(User, args); 2344 | 2345 | console.log(abhilash instanceof User); //true 2346 | console.log(abhilash.fullName); // Abhilash Bhasin 2347 | ``` 2348 | 2349 | **Example:** Get property of an object using `Reflect.get()` 2350 | 2351 | ```js 2352 | const user = { 2353 | name: 'Abhilash', 2354 | age: 28 2355 | }; 2356 | console.log(Reflect.get(user, 'age')); // 33 2357 | ``` 2358 | 2359 | **Example:** Calling a function using `Reflect.apply()` 2360 | 2361 | ```js 2362 | const max = Reflect.apply(Math.max, Math, [10, 20, 30]); 2363 | 2364 | console.log(max); 2365 | ``` 2366 | 2367 | **Example:** Delete property using `Reflect.deleteProperty()` 2368 | 2369 | ```js 2370 | const user = { 2371 | name: 'Abhilash', 2372 | age: 28 2373 | }; 2374 | 2375 | console.log(Reflect.deleteProperty(user, 'age')); // true 2376 | console.log(user.age); // undefined 2377 | ``` 2378 | 2379 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-reflect-e7euzj?file=/src/index.js)** 2380 | 2381 |
2382 | ↥ back to top 2383 |
2384 | 2385 | ## # 14.1. Unicode 2386 | 2387 | Prior to ES6, JavaScript strings are represented by 16-bit character encoding (UTF-16). Each character is represented by 16-bit sequence known as code unit. 2388 | 2389 | ECMAScript 6 added full support for UTF-16 within strings and regular expressions. It introduces new Unicode literal form in strings and new RegExp flag **\u** mode to handle code points, as well as new APIs(codePointAt, fromCodePoint) to process strings. 2390 | 2391 | **Example:** 2392 | 2393 | ```js 2394 | /** 2395 | * Unicode 2396 | */ 2397 | let str = '𠮷'; 2398 | 2399 | // new string form 2400 | console.log('\u{20BB7}'); // "𠮷" 2401 | 2402 | // new RegExp u mode 2403 | console.log(new RegExp('\u{20BB7}', 'u')); 2404 | console.log(/^.$/u.test(str)); // true 2405 | 2406 | //API methods 2407 | console.log(str.codePointAt(0)); // 134071 2408 | console.log(str.codePointAt(1)); // 57271 2409 | 2410 | console.log(String.fromCodePoint(134071)); // "𠮷" 2411 | ``` 2412 | 2413 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-unicode-ve845j?file=/src/index.js)** 2414 | 2415 |
2416 | ↥ back to top 2417 |
2418 | 2419 | ## # 14.2. Proper Tail Calls 2420 | 2421 | **Proper tail call ( PTC )** is a technique where the program or code will not create additional stack frames for a recursion when the function call is a tail call. 2422 | 2423 | For example, the below classic or head recursion of factorial function relies on stack for each step. Each step need to be processed upto `n * factorial(n - 1)` 2424 | 2425 | ```js 2426 | function factorial(n) { 2427 | if (n === 0) { 2428 | return 1 2429 | } 2430 | return n * factorial(n - 1) 2431 | } 2432 | 2433 | console.log(factorial(5)); //120 2434 | ``` 2435 | 2436 | But if you use **Tail recursion functions**, they keep passing all the necessary data it needs down the recursion without relying on the stack. 2437 | 2438 | The browsers which supports PTC do not generate stack overflow instead shows Infinity with below inputs, 2439 | 2440 | ```js 2441 | function factorial(n, acc = 1) { 2442 | if (n === 0) { 2443 | return acc; 2444 | } 2445 | return factorial(n - 1, n * acc); 2446 | } 2447 | 2448 | console.log(factorial(5)); //120 2449 | console.log(factorial(10)); // 3628800 2450 | console.log(factorial(100)); // 9.332621544394418e+157 2451 | console.log(factorial(1000)); // Infinity 2452 | ``` 2453 | 2454 | **⚝ [Try this example on CodeSandbox](https://codesandbox.io/s/es6-proper-tail-calls-qmptbq?file=/src/index.js)** 2455 | 2456 |
2457 | ↥ back to top 2458 |
2459 | -------------------------------------------------------------------------------- /assets/JavaScript-Promise-Fulfilled.svg: -------------------------------------------------------------------------------- 1 | PendingFulfilled: valueTime -------------------------------------------------------------------------------- /assets/JavaScript-Promise-Rejected.svg: -------------------------------------------------------------------------------- 1 | PendingRejected: errorTime -------------------------------------------------------------------------------- /assets/promise-then-chain.svg: -------------------------------------------------------------------------------- 1 | .thennew Promiseresolve(1)return 2.thenreturn 4.then -------------------------------------------------------------------------------- /assets/star.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/learning-zone/javascript-es6-basics/1ff6ae38e2ba9e64ba41970827dc02773754bcf3/assets/star.png -------------------------------------------------------------------------------- /es6/async-in-es6.js: -------------------------------------------------------------------------------- 1 | //01. Example:- Promises 2 | 3 | const delay = seconds => { 4 | return new Promise(resolve => { 5 | setTimeout(resolve, seconds * 1000) 6 | }); 7 | }; 8 | 9 | console.log("Zero seconds wait"); 10 | delay(1).then(() => console.log('One seconds wait')); 11 | delay(5).then(() => console.log('Five seconds wait')); 12 | 13 | //02. Loading data with fetch() 14 | 15 | const getPeopleInSpace = () => 16 | fetch('http://api.open-notify.org/astros.json') 17 | .then(res => res.json()); 18 | 19 | getPeopleInSpace() 20 | .then(console.log); 21 | 22 | //03. Async() and await() 23 | 24 | const delay = seconds => { 25 | return new Promise( 26 | resolve => setTimeout(resolve, seconds * 1000) 27 | ) 28 | }; 29 | 30 | const countToFive = async() => { 31 | console.log('zero seconds wait'); 32 | await delay(2); 33 | console.log('Two seconds wait'); 34 | await delay(5); 35 | console.log('Five seconds wait'); 36 | } 37 | 38 | countToFive(); 39 | 40 | //04. Async with fetch 41 | 42 | const githubRequest = async(loginName) => { 43 | try{ 44 | var response = await fetch(`http://api.github.com/users/${loginName}/followers`); 45 | var json = await response.json(); 46 | var followerList = json.map(user => user.login); 47 | console.log(followerList); 48 | } catch(e){ 49 | console.log("Data didn't load", e); 50 | } 51 | }; 52 | 53 | //githubRequest('eveporcello'); 54 | githubRequest('pradeepkumar2'); 55 | -------------------------------------------------------------------------------- /es6/es6-features.js: -------------------------------------------------------------------------------- 1 | //01. Template String 2 | 3 | function print(Name) { 4 | console.log("Hello " + Name); 5 | console.log(`Hello ${Name}`); 6 | } 7 | print("Pradeep"); 8 | 9 | 10 | //02. Spread Operators 11 | 12 | var firstGroup = ["C", "C++", "Java"]; 13 | var secondGroup = ["SQL", "MySQL", "BigData"]; 14 | var thirdGroup = ["Android", "Python", "Ruby", firstGroup, secondGroup]; 15 | var finalGroup = ["Android", "Python", "Ruby", ...firstGroup, ...secondGroup]; 16 | 17 | console.log(thirdGroup); 18 | console.log(finalGroup); 19 | 20 | 21 | //03. Sets 22 | 23 | const sets = new Set([1, 2, 3, 4, 5]); 24 | 25 | console.log(sets.has(1)); 26 | console.log(sets.has(0)); 27 | 28 | 29 | //04. Default Parametrs 30 | 31 | function add(x = 10, y = 20) { 32 | console.log(x + y); 33 | } 34 | add(30, 40); 35 | 36 | 37 | //05. repeat() 38 | 39 | var cat = { 40 | meow(times){ 41 | console.log("meow ".repeat(times)); 42 | } 43 | }; 44 | cat.meow(2); 45 | 46 | 47 | //06. Arrow Function 48 | 49 | var companyList = function(company) { 50 | console.log(company); 51 | }; 52 | 53 | //ES-6 54 | var companyList = company => console.log(company); //arraow function 55 | 56 | companyList(["Apple", "Microsoft", "Google"]); 57 | 58 | 59 | //07. Arrow function with 'this' 60 | 61 | var person = { 62 | first: "Pradeep", 63 | actions: ["bike", "hike", "ski", "surf"], 64 | printActions: function() { 65 | var _this = this; 66 | this.actions.forEach(function(action) { 67 | var str = _this.first + " likes to " + action; 68 | console.log(str); 69 | }); 70 | } 71 | }; 72 | person.printActions(); 73 | 74 | //ES-6 75 | var person = { 76 | first: "Pradeep", 77 | actions: ["bike", "hike", "ski", "surf"], 78 | printActions() { 79 | this.actions.forEach(action => { 80 | var str = this.first + " likes to " + action; 81 | console.log(str); 82 | }); 83 | } 84 | }; 85 | 86 | 87 | //08. Destructing Assignment 88 | 89 | var phone = { 90 | title: "iPhone", 91 | price: 800, 92 | description: "The iPhone is a smartphone developed by Apple" 93 | }; 94 | 95 | console.log(phone.title); 96 | //ES-6 97 | var { title, price } = { 98 | title: "iPhone", 99 | price: 800, 100 | description: "The iPhone is a smartphone developed by Apple" 101 | }; 102 | 103 | console.log(title); 104 | 105 | //09. Generators 106 | 107 | /*** Calling a generator function does not execute its body immediately ***/ 108 | 109 | function* generator(i) { 110 | yield i; 111 | yield i + 10; 112 | } 113 | var gen = generator(10); 114 | console.log(gen.next().value); 115 | console.log(gen.next().value); 116 | 117 | 118 | //10. Symbols 119 | 120 | /*** They are tokens that serve as unique IDs. We create symbols via the factory function Symbol() ***/ 121 | 122 | const symbol1 = Symbol(); 123 | const symbol2 = Symbol(42); 124 | const symbol3 = Symbol('foo'); 125 | 126 | console.log(typeof symbol1); 127 | 128 | console.log(symbol3.toString()); 129 | 130 | console.log(Symbol('foo') === Symbol('foo')); 131 | 132 | 133 | // 11. Iterator 134 | 135 | /** The iterable protocol allows JavaScript objects to define or customize their iteration behavior. **/ 136 | 137 | var title = 'ES6'; 138 | var iterateIt = title[Symbol.iterator](); 139 | 140 | console.log(iterateIt.next().value); //output: E 141 | console.log(iterateIt.next().value); //output: S 142 | console.log(iterateIt.next().value); //output: 6 --------------------------------------------------------------------------------