Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

README.md

ES6, also known as ECMAScript 2015, is a major update to the JavaScript language, introducing many new features and improvements. In this day, we will cover some of the most commonly used features of ES6.

Template Literals

Template literals are a new way to create strings in JavaScript. They allow you to embed expressions inside the string, making it easy to create dynamic strings. Template literals are enclosed by backticks ( ` ).

Here's an example:

const name = "John";
const age = 30;
const message = `My name is ${name} and I am ${age} years old.`;
console.log(message);

In the example above, we created a template literal that includes the name and age variables inside the string using ${}. When we log the message variable to the console, it will print the string with the values of the variables.

Arrow Functions

Arrow functions are a shorthand way to define functions in JavaScript. They have a more concise syntax than regular functions and they also have a lexically bound this keyword, which means that the this keyword inside an arrow function always refers to the parent scope.

Here's an example:

const numbers = [1, 2, 3, 4, 5];

const doubledNumbers = numbers.map(number => number * 2);

console.log(doubledNumbers);

In the example above, we defined an arrow function that takes a number parameter and returns the doubled value of that number. We then used the map method to apply this function to each element in the numbers array, resulting in a new array with the doubled values.

Default Parameters

Default parameters allow you to set default values for function parameters. If a value is not passed to a parameter when the function is called, the default value will be used instead.

Here's an example:

function greet(name = "John") {
  console.log(`Hello, ${name}!`);
}

greet(); // logs "Hello, John!"
greet("Jane"); // logs "Hello, Jane!"

In the example above, we defined a greet function that takes a name parameter with a default value of "John". If no value is passed to the name parameter when the function is called, the default value will be used instead.

Destructuring

Destructuring is a way to extract values from objects and arrays and assign them to variables.

Here's an example:

const person = {
  name: "John",
  age: 30,
  address: {
    city: "New York",
    country: "USA"
  }
};

const { name, age, address: { city, country } } = person;

console.log(name); // logs "John"
console.log(age); // logs 30
console.log(city); // logs "New York"
console.log(country); // logs "USA"

In the example above, we defined a person object with name, age, and address properties. We then used destructuring to extract the name, age, city, and country properties and assign them to variables with the same name.

Spread Operator

The spread operator (...) allows you to spread the elements of an array or object into another array or object.

Here's an example:

const numbers1 = [1, 2, 3];
const numbers2 = [4, 5, 6];

const combinedNumbers = [...numbers1, ...numbers2];

console.log(combinedNumbers); // logs [1, 2, 3, 4, 5, 6]

In the example above, we used the spread operator to combine the elements of numbers1 and numbers2 arrays into a new combinedNumbers array.

Rest Operator

The rest operator (...) allows you to represent an indefinite number of arguments as an array.

Here's an example:

function sum(...numbers) {
  return numbers.reduce((total, number) => total + number);
}

console.log(sum(1, 2, 3)); // logs 6
console.log(sum(4, 5, 6, 7)); // logs 22

In the example above, we defined a sum function that takes an indefinite number of arguments using the rest operator. Inside the function, we used the reduce method to add up all the numbers and return the total.


Here are some more ES6 features that you might find useful in real-world projects:

1. Classes

ES6 introduces a new way to define classes in JavaScript. Classes are a template for creating objects and have a more concise syntax than the traditional constructor function syntax.

Here's an example:

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

  greet() {
    console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
  }
}

const john = new Person("John", 30);
john.greet(); // logs "Hello, my name is John and I am 30 years old."

In the example above, we defined a Person class with a constructor method that sets the name and age properties of a new instance of the class. We also defined a greet method that logs a greeting message to the console. We then created a new john object using the Person class and called the greet method on it.

2. Modules

ES6 introduces a new way to organize and share code between files using modules. Modules are a way to encapsulate code and export and import functions, objects, and other values between files.

Here's an example:

// math.js file
export function add(a, b) {
  return a + b;
}

export function subtract(a, b) {
  return a - b;
}

// app.js file
import { add, subtract } from "./math.js";

console.log(add(1, 2)); // logs 3
console.log(subtract(4, 2)); // logs 2

In the example above, we defined two functions in the math.js file and exported them using the export keyword. We then imported these functions in the app.js file using the import keyword and called them.

3. Promises

Promises are a new way to handle asynchronous operations in JavaScript. Promises represent a value that may not be available yet and provide a way to handle the success or failure of an asynchronous operation.

Here's an example:

function fetchData(url) {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();

    xhr.onload = () => {
      if (xhr.status === 200) {
        resolve(xhr.response);
      } else {
        reject(new Error("Failed to fetch data"));
      }
    };

    xhr.open("GET", url);
    xhr.send();
  });
}

fetchData("https://jsonplaceholder.typicode.com/posts")
  .then(response => console.log(response))
  .catch(error => console.error(error));

In the example above, we defined a fetchData function that returns a new promise that resolves with the response data if the request is successful or rejects with an error if the request fails. We then called this function and used the then and catch methods to handle the success or failure of the promise.

4. Async/Await

Async/await is a new way to write asynchronous code that is built on top of promises. Async functions allow you to write asynchronous code that looks like synchronous code, making it easier to read and understand.

Here's an example:

async function fetchData(url) {
  const response = await fetch(url);
  const data = await response.json();
  return data;
}

fetchData("https://jsonplaceholder.typicode.com/posts")
  .then(data => console.log(data))
  .catch(error => console.error(error));

In the example above, we defined an async function called fetchData that uses the await keyword to wait for the fetch and json methods to complete before returning the data. We then called this function and used the then and catch methods to handle the success or failure of the promise.

5. Map, Filter, and Reduce

ES6 introduces new array methods map, filter, and reduce that make it easier to work with arrays and perform common operations like transforming, filtering, and reducing data.

Here's an example:

const numbers = [1, 2, 3, 4, 5];

const doubledNumbers = numbers.map(number => number * 2);
console.log(doubledNumbers); // logs [2, 4, 6, 8, 10]

const evenNumbers = numbers.filter(number => number % 2 === 0);
console.log(evenNumbers); // logs [2, 4]

const sum = numbers.reduce((accumulator, number) => accumulator + number, 0);
console.log(sum); // logs 15

In the example above, we defined an array of numbers and used the map method to double each number in the array, the filter method to filter out odd numbers, and the reduce method to calculate the sum of all the numbers in the array.

6. Classes and Inheritance

ES6 introduces a new syntax for defining classes and inheritance in JavaScript. This makes it easier to write object-oriented code and is similar to the class syntax found in other programming languages like Java and C++.

Here's an example:

class Animal {
  constructor(name) {
    this.name = name;
  }

  speak() {
    console.log(`${this.name} makes a noise.`);
  }
}

class Dog extends Animal {
  constructor(name) {
    super(name);
  }

  speak() {
    console.log(`${this.name} barks.`);
  }
}

const dog = new Dog('Rufus');
dog.speak(); // logs "Rufus barks."

In the example above, we defined a base class Animal with a constructor and a method called speak. We then defined a subclass Dog that extends the Animal class and overrides the speak method with a different implementation. We then created an instance of the Dog class and called its speak method to see the output.