Posts

Showing posts with the label es6

ES6- Spread Operator

Image
       Spread Operator(...) in ES6   Using the Spread Operator, you can copy a part of an existing array into another array. The spread operator, one of new feature added in the JavaScript ES6 version.   < script> const arr1 = [ 'a' , 'b' , 'c' ]; const arr2 = [ 'd' , 'e' , 'f' ]; const arrCombined = [...arr1, ...arr2]; document .write(arrCombined); < /script>   The spread operator with destructuring. Example   Assign the first and second items from numbers to variables and put the rest in an array: const arr1 = [ 1 , 2 , 3 , 4 , 5 , 6 ]; const [one, two, ...rest] = arr1;     spread operator with functions      TOP If the spread operator is used as a parameter, it is known as the rest parameter. You can also accept multiple arguments in a function call using the rest parameter .  Example,   let func1 = function (...args) { console.log(args); } func1( 3 ); // [3] func1( 4 , 5 , 6 ); ...

Es6 Module

Image
Export-Import in ES6 JavaScript's ES6 allow you to organize the code components in the form of modules and can be import them into other modules. This makes code maintenance easier. If they are written in separate files. With the help of import and export keywords, Modules can be used in other programs. Using Export You can export a function or variable from any file. Let us create a file named employee.js, and fill it with the things we want to export. There are two types of exports: 1. Named and 2. Default. Named exports must be enclosed using curly braces. Default exports do not. Named Exports You can create named exports two ways. 1. In-line individually, or 2. All at once at the bottom. MOVETOP↑   Example In-line individually: export const empname = "benny" export const empage = 30 // first declare variable and export let empcompany = "xyz" export {empcompany } All at once at the bottom: employeerecord.js // first declare variable and export cons...

Es6 Destructuring

Image
Es6 Destructuring  ES6HOME What is Destructuring Destructuring makes it easy to extract array values from any index. Using Destructuring With destructing, we can assign array items to variables:   const userdata = [ 'John' , 'john@gmail.com' , '123451234' ]; const [name,email, phone] = userdata ; // assiging array values to different variables.     When destructuring arrays, Keep in mind the order of the variables that you are providing. To extract only name,phone write the following way. const [name,,phone] = userdata ;       MOVETOP↑   Destructuring with Array Default values The following example shows how Array Destructuring Assignment will work with array Default value. const userdata = [ 'John' , 'john@gmail.com' , '123451234' ]; onst [name = "John" , email = "john@gmail.com" ] = [ "Doe" ]; console.log(name); // Deo console.log(email); // john@gmail.com     Using array destructurning...

Es6 Array Functions

Image
 ES6 Array Methods     ES6HOME Es6 provided us many cool functions which helps us to code better. So in this blog we are going to discuss some of the best array functions. These functions can help you to develop efficient code with reduced complexity. So let's get started! concat() :  This method will combine two or more arrays and produce one single array object. const arr1 = [ 'a' , 'b' , 'c' ]; const arr2 = [ 'x' , 'y' , 'z' ];   const arr3 = arr1.concat(arr2); consol.log(arr3);   output:     ['a','b','c','x','y','z'];       map():     This method operated on each element in the array, and finally produce the new array with results.   const arr1 = [ '1' , '2' , '3' ]; const arr2 = arr1.map((x) => x * 2 );      output:      2,4,6         ES6HOME filter(): This filter function reads each element of the array and compares it with the given conditio...

ES6 Tutorial

Image
JavaScript ES6 Tutorial   What is ES6? ES6 known as ECMAScript6, It is advanced version o JavaScript, introduced to standardized JavaScript . It was published in 2015, that's the reason it is also called as ECMAScript2015. It is added some new features to JavaScript. Some popular features of the ECMAScript 6 are as follows… Classes Arrow Functions Variables (let, const, var) Array Functions Modules Destructuring Spread Operator   Popular ES6 frameworks such as ReactJS, AngularJS, and Vue.js implemented these features     ES6 Classes:   ES6 introduced classes. A class starts with a class keyword. Class contains constructor(). Inside, the constrictor properties are initialized.   class Person{ constructor(name) { this .pname = name; } } Create an object called "pobj" based on the Car class: const pobj = new Person( "John" );     demo.html    <!DOCTYPE html> <html> <body> <script> class Person{ ...