ECMAScript 2018

ECMAScript 2018


ECMAScript 2018
 
In this tutorial, you will learn about ECMAScript 2018 or ES 2018:-
Introducing the new features that ECMAScript 2018 (ES9) adds to JavaScript :-
 
  • Asynchronous iteration
  • Rest/Spread operators for variables
  • finally() for promises
  • Regex improvements
 
JavaScript Rest Operator
 
JavaScript Rest Operator represented by (….) as same as spread operator. The main difference between rest and spread is that the rest operator puts the rest of some specific user-supplied values into a JavaScript array. But the spread syntax expands iterables into individual elements using rest parameters is describing below:
  • Rest parameters represent an indefinite number of arguments as an array.
  • Rest parameters of the function prefixed with(…).
  • The Rest parameters index starts with zero index.
  • Rest parameters must be the last parameter of function arguments.
 
Syntax
 
function function_Name(…restParams){
             // function body
}
function_Name()       // calling function
 
Example – 
 
https://lh6.googleusercontent.com/nUV6_zyvgWFhZ9fo1xknDNc49vAz5BZZ-XIiwqNThTJnMdqDJ1RkIpwW7S3K5O61sEfWYVLzT_Ro-1hJZU87f68X_Axurkh7kkj3fMnSvQtXfp-57zkAeq764IEbcYZSWvKThCq7
 
Output -
 
Rest Operators
10
15
 
JavaScript Spread Operators
 
Spread is an array/object expiration to be expanded in places where zero more key value pairs are expected. Spread allows iterators over the arguments. 
 
Spread in Function calls 
  • Spread is working as a replacement for the apply function.
  • Spread is used to pass an array of arguments to a function.
 
The Syntax Spread function is as follows:
Functioncall(…iterableObject);
 
Spread in array literals
 
Array concatenation using spread literals.
The Syntax of Spread in array concatenation using spread literals is as follows:
var arrObj1 = [1,2,3,4,5];
var arrObj2 = [6,7,8,9,10];
var combined = […arrObj1, … arrObj2 ]
console.log(combined) 
 
Example -
 
https://lh3.googleusercontent.com/70fY78VzmaCMsD37BxInH79ElhdeZeG3eJjTqFDPXD5CCoZFvcoo5wnws6JCPjTHgLvjZWN4cSNKtTcp8nwmd_R35vFe-LQNDHRdP8xzjAh8x3Lat35LKhtGOTTj0rlJeFt3tgcx
 
Output -
 
Spread Operators
Apple,Grapes,Banana
 
JavaScript Regular Expressions
 
Regular Expression (RegEx) is an object that describes a sequence of characters used defining a search pattern. 
 
Creating a Regular Expression
 
You construct a Regular Expression in one of two ways:
  • Using a regular expression literal, which consists of a pattern enclose between slashes, as follows:
 
const re = /ab+c/;
Regular expression literals provide compilation of the regular expression when the script is loaded. If the regular expression remains constant, using this can improve performance.\
 
  • Or calling the constructor function of the RegExp object, as follows :
Const re = new RegExp(‘ab+c’);
 
 
Example 1 – Do a case-insensitive search for “webdesigninghouse” in a string:
 
https://lh6.googleusercontent.com/j3HVpFb4TK0Fhf2As9XCd1Lhq4R5Kd4gyf4Me2xKYggLtLqsV8Up1-_ybntzoQLQ4B_rnkpcklPbr80sHME44UKVi621rRCb8f2KrvhMSgtyKLuzyNcrbZXTFVptXKZSYM5HWVgA
 
Output – 
 
https://lh4.googleusercontent.com/8cczHbHnQWXSEYDFAHGFAQmzAzWAmUNeIly-oS5yrcmPSoyAWLaUO-q9aJmMU_3WUnSPQgYrWPVm4E6QPYablm9ZGPllAZ2B_ASnsvh0pwW-IBaIHVn5pqubFgFdW4O8iIoyYuIW
 
 
Example 2 - Calling the constructor function of the RegExp object, as follows:
 
https://lh5.googleusercontent.com/JGXwqcyCULrib5SlJpg8_yr3XCRUsF66KbL902bsgEU7wa7zF-JbDexSN3tfu_uk-c7pQZPSbyHOOmBXyoAatWYECD7raXk7164XVdupOmpGGJqN_4TGhAIdiDX07I_2TxDqd8SB
 
Output –
 
Before clicking the button:
https://lh4.googleusercontent.com/W_2kYFFkGLTIzdhCsSdMjHDd40FLQ3JW5qyyJW4wwuLzrP0ecD_kv0a94LJQGZg7Q47QGBpoWp4lJK8FP9DjlB2l0CIfZtR85UIysU7QhsFnZwX6zyzJ-ktevqsOUufZhWZAayV4
 
After clicking the button:
 
https://lh5.googleusercontent.com/bqfj3jmkHtXOb7pxdXQEFIS-gv_Mul-8-akqjiILYY57gJR0mVMPGlVAHbtFCfcI4KavNpf4G8mc0FkR53eMFNwSMfzXlU1UxszFul9tHLS9SgHzQ_ez3XgA_b-8r4OKGjP76AeR