DEV Community

Cover image for Javascript - Introduction to ES6
Ibrahim Bagalwa
Ibrahim Bagalwa

Posted on

Javascript - Introduction to ES6

ECMAScript 6 (ES6) is the latest version of JavaScript and offers a wealth of new features and improvements to the language. ES6 is now widely supported across modern browsers and is the future of JavaScript development. In this article, we'll take a closer look at some of the most important features of ES6 and how they can make your code more efficient, maintainable, and readable.

const and let variables

ES6 introduces two new variable declarations, let and const, which offer more control over variable scope.

  • let allows you to declare variables within a block scope, while
  • const is used for declaring constants that cannot be reassigned.

The following examples illustrates the difference between let and const:

let number= 10;
number= 20;
console.log(number); // 20

const PI = 3.14;
PI = 3.15;
console.log(PI); 
// PI = 3.15;
   ^
//TypeError: Assignment to constant variable.
Enter fullscreen mode Exit fullscreen mode
let x = 5;
if (true) {
  let y = 6;
  console.log(x + y); // 11
}
console.log(y); 
//console.log(y);
            ^
//ReferenceError: y is not defined
Enter fullscreen mode Exit fullscreen mode

Exercise:

  1. Try declaring a variable using let and change its value within the same block scope.
  2. Try declaring a variable using const and try to change its value.

Arrow functions

ES6 introduces a new syntax for creating functions called arrow functions.
Arrow functions allow us to write shorter function syntax and makes your code more readable, more structured, and look like modern code.

const greet = name => console.log(`Hello, ${name}!`);
greet('Ibrahim'); // Hello, Ibrahim!

const numbers = [1, 2, 3, 4];
const square = numbers.map(n => n * n);
console.log(square); // [1, 4, 9, 16]
Enter fullscreen mode Exit fullscreen mode

Exercise:

  1. Create an array of numbers and use an arrow function with the map() method to square each number in the array.
  2. Try using arrow functions in an array method such as filter, javascript reduce, or sort ##Template Literals ES6 introduces a new syntax for creating template literals or template strings, which allow you to embed expressions in string literals.This makes concatenation of strings much easier and more readable. The following example illustrates the use of template literals:
let name = "Ibrahim";
let message = `Hello, ${name}!`;
console.log(message); // Hello, Ibrahim!
Enter fullscreen mode Exit fullscreen mode

Exercise:

  • Create a template literal that concatenates two strings.

Default parameters

Default function parameter allow named parameters to be initialized with default values if no value or undefined is passed (you define a parameter in advance).
The following example illustrates the use of default parameter

const myDefaultParameter = (name = "Jack", age = 22) => {
  return `${name} is ${age}`;
};

console.log(myDefaultParameter()); //Jack is 22
console.log(myDefaultParameter("Olivier", 25)); //Olivier is 25
Enter fullscreen mode Exit fullscreen mode

Destructuring

Destructuring allows you to extract values from arrays or objects into variables. This makes it easier to work with data structures and reduces the need for repetitive code.

//Oject
const student = { name: 'John', age: 25, grade: 'A' };
const { name, age } = student;
console.log(name, age); // John 25

//Array
const numbers = [1, 2, 3, 4];
const [first, second, ...rest] = numbers;
console.log(first, second, rest); // 1 2 [3, 4]
Enter fullscreen mode Exit fullscreen mode

Exercise:

  1. Create an object and an array and use destructuring to extract values into separate variables.

Spread and Rest Operators

Spread operator allows you to spread elements of an array into a new one, and the rest operator, allows you to collect multiple elements into an array.

//spread operator
const numbers = [1, 2, 3];
const newNumbers = [...numbers, 4, 5];
console.log(newNumbers); // [1, 2, 3, 4, 5]

//rest operators
function add(...args) {
  return args.reduce((sum, number) => sum + number, 0);
}
console.log(add(1, 2, 3)); // 6
Enter fullscreen mode Exit fullscreen mode

Modules

Modules allow you to export and import functions, objects, or values between different files, making it easier to write reusable code. The following example illustrates the use of modules:

// myModule.js
export const add = (a=0, b=0) => a + b;
export const multiply = (a=1, b=1) => a * b;

// index.js
import { add, multiply } from "./myModule";
console.log(add(1, 2)); // 3
console.log(multiply(2, 3)); // 6
Enter fullscreen mode Exit fullscreen mode

Promises

Allow you to write asynchronous code. It can be used when, we want to fetch data from an API, or when we have a function that takes time to be executed, etc

const promise = ()=>{
    return new Promise((resolve,reject)=>{
        resolve("Promise resolve successfully!")
    })
}
console.log(promise())//Promise { 'Promise execute successfully!' }
Enter fullscreen mode Exit fullscreen mode

Promises it a big topic in javascript es6 if you need to learn more about Promises, left a comment.

Classes

Class makes it easier to write object-oriented code. Classes are used to create objects that have methods and properties, and they allow you to encapsulate and reuse code.
The following example illustrates the use of classes:

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

  greet() {
    console.log(`Hello, I am ${this.name}`);
  }
}

const josh = new Person("Josh", 30);
josh.greet(); // Hello, I am Josh
Enter fullscreen mode Exit fullscreen mode

Conclusion

ES6 provides a number of new features and improvements that make JavaScript a more powerful and efficient language. The new let and const variables, arrow functions, template literals, destructuring, spread and rest operators, modules, default parameters, promises and classes, make it easier to write clean, readable, and maintainable code. By incorporating these new features into your projects, you can take advantage of the benefits they provide and build more powerful and sophisticated applications.

I hope you guys found this article useful, and I hope I was able to introduce you some of the ES6 features.
Thank you for taking the time to read it & do let me know your feedback on this article.

Top comments (9)

Collapse
 
edsonpro profile image
edson-pro

Good one

Collapse
 
ivadyhabimana profile image
Ivad Yves HABIMANA

Nice one! Didn't know about Default parameters.

Collapse
 
ibrahimbagalwa profile image
Ibrahim Bagalwa

thanks

Collapse
 
kawede profile image
kawede Ezechiel • Edited

Hello, i have read this article with much intention, it is an interesting one. i hope i will share it too on my platform. Congratulations!!!!

Collapse
 
ibrahimbagalwa profile image
Ibrahim Bagalwa

thanks a lot

Collapse
 
davmaene profile image
David Maene

Great Job, Dear Ibrahim Bagalwa

Collapse
 
ibrahimbagalwa profile image
Ibrahim Bagalwa

thanks

Collapse
 
jenckw profile image
Jenny Chikwanine

Interesting, let’s share.Congrats

Collapse
 
ibrahimbagalwa profile image
Ibrahim Bagalwa

Thanks a lot