ES6 Features: write less, do more

Search for a command to run...

No comments yet. Be the first to comment.
Before beginning our quest to find how not to use Create React App. First, letās see why we need Create React App in the first place. Why do we need Create React App? š¤ Create React App is a comfortable environment for learning React and is the best...

Getting Started with Node.js In this blog, we will try to understand the basics of Node.js, how it works, and why to choose Node.js, but before diving into node let's talk a little bit about Java-script. JavaScript is one of the best programming lang...

If you are reading this, you probably know what React.js is and might have already used it Earlier. However, you might be wondering why I am reading about folder structure? Canāt I just stuff all my files in the src folder?? 𤪠Technically you could ...

Letās first understand š¤ What is an Array? Quoting from MDN directly. The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name and has members for performing common...

JavaScript has progressed a ton in recent years. If you are learning JavaScript in 2022 and you have not touched ES6, youāre missing out on an easier way to read and write JavaScript. š
We as developers must be trying to use the best tools and features in our disposition to make our job easy and efficient.
In this blog, we will discuss some of the best and most popular ES6 features that we use everyday in JavaScript coding.
Letās Start š š š
let value=19;
value=18;
//ES5
function myFunction(){
console.log("hello");
}
//ES6
const myFunction = () => {
console.log("hello");
}
For concatenation, earlier for ES5 we use (+) operator but in ES6 they are enclosed in
backticks **` `**
We can use a new syntax ${PARAMETER} inside of the back-ticked string.
EXAMPLE
ES5
let myName = 'pankaj';
console.log("My name is " + myName); // My name is pankaj
ES6
let myName = 'pankaj';
console.log(`My name is ${myName}`); // My name is pankaj
//string substitution
var greeting = `\`hello\` World!`;
Example
ES5
let message=`Before ES6, you use the following technique to create
a multi-line string by manually including the newline
character ( **\n**) in the string and (+) plus operator.`
ES6
let message='Before ES6, you use the following technique to create \n'
+ 'a multi-line string by manually including the newline\n'
+ 'character ( **\n**) in the string and (+) plus operator.'
The output of both these programs will be the same.
output
Before ES6, you use the following technique to create
a multi-line string by manually including the newline
character ( **\n**) in the string and (+) plus operator.
Example
Copy Array Using Spread Operator
const arr1 = ['a', 'b'];
const arr2 = [...arr1, 'c', 'd', 'e'];
console.log(arr2);
// Output:
// ["a", "b", "c", "d", ""]
Example
'.src/function'
export function double(n){
return n*2;
}
import {double} from '.src/function'; //import double function
console.log(double(5));//10
Example
function say(message) {
console.log(message);
}
say(); // undefined
function say(message='Hi') {
console.log(message);
}
say(); // 'Hi'
say('Hello') // 'Hello'
ES5
// assigning object attributes to variables
const person = {
name: 'pankaj',
age: 21,
gender: 'male'
}
let name = person.name;
let age = person.age;
let gender = person.gender;
console.log(name); // pankaj
console.log(age); // 21
console.log(gender); // male
ES6
// assigning object attributes to variables
const person = {
name: 'pankaj',
age: 21,
gender: 'male'
}
// destructuring assignment
let { name, age, gender } = person;
console.log(name); // pankaj
console.log(age); // 21
console.log(gender); // male
Was this article helpful for you? Let me know in the comments section if you have any questions or thoughts! I would love to hear them. š