This document explains ES6 features with examples.
π Baca dalam Bahasa Indonesia
- Introduction
- Let and Const
- Arrow Functions
- Template Literals
- Destructuring Assignment
- Default Parameters
- Rest and Spread Operators
- Classes
- Modules
- Promises and Async/Await
- Map and Set in ES6
- Generator
- Conclusion
ES6 (ECMAScript 6) is the sixth version of JavaScript, released in 2015. It introduced many new features that make JavaScript easier and cleaner to write, enhancing development experience and application performance. Frameworks like React, Angular, and Vue heavily rely on ES6 features.
let
allows variable declaration within a block scope.
let animal = "Tiger";
animal = "Elephant"; // Allowed
console.log(animal); // Elephant
const
declares a constant variable that cannot be reassigned.
const kingOfJungle = "Lion";
// kingOfJungle = "Wolf"; // Error: Assignment to constant variable
console.log(kingOfJungle);
Arrow functions provide a shorter syntax for writing functions.
const roar = (animal) => `${animal} is roaring!`;
console.log(roar("Bear")); // Bear is roaring!
Template literals allow expressions and multiline strings.
const animal = "Deer";
const message = `The ${animal} runs fast in the forest.`;
console.log(message);
const jungleAnimals = ["Monkey", "Tiger", "Owl"];
const [first, second, third] = jungleAnimals;
console.log(first); // Monkey
console.log(second); // Tiger
const bird = { name: "Eagle", speed: "Fast" };
const { name, speed } = bird;
console.log(name); // Eagle
console.log(speed); // Fast
const animalSound = (animal = "Wolf") => `${animal} howls at night.`;
console.log(animalSound()); // Wolf howls at night.
console.log(animalSound("Fox")); // Fox howls at night.
const jungleAnimals = ["Tiger", "Snake", "Jaguar"];
const allAnimals = ["Elephant", ...jungleAnimals, "Deer"];
console.log(allAnimals);
const describeAnimals = (first, ...others) => {
console.log(`First animal: ${first}`);
console.log(`Other animals: ${others.join(", ")}`);
};
describeAnimals("Leopard", "Crocodile", "Eagle", "Bear");
class Animal {
constructor(name, habitat) {
this.name = name;
this.habitat = habitat;
}
description() {
return `${this.name} lives in ${this.habitat}.`;
}
}
const panda = new Animal("Panda", "Bamboo Forest");
console.log(panda.description());
// file: animal.js
export const jungleBird = "Parrot";
export function fly(animal) {
return `${animal} is flying high!`;
}
// file: main.js
import { jungleBird, fly } from "./animal.js";
console.log(jungleBird); // Parrot
console.log(fly("Hawk")); // Hawk is flying high!
const findFood = new Promise((resolve, reject) => {
setTimeout(() => resolve("Bear found honey!"), 2000);
});
findFood.then((message) => console.log(message));
const findWater = async () => {
return "Deer found a river!";
};
findWater().then(console.log);
const animalSpeed = new Map();
animalSpeed.set("Cheetah", "Fastest");
animalSpeed.set("Turtle", "Slow");
console.log(animalSpeed.get("Cheetah")); // Fastest
const uniqueAnimals = new Set(["Wolf", "Fox", "Wolf"]);
console.log(uniqueAnimals.size); // 2
function* animalGenerator() {
yield "Elephant";
yield "Tiger";
yield "Giraffe";
}
const animals = animalGenerator();
console.log(animals.next().value); // Elephant
console.log(animals.next().value); // Tiger
console.log(animals.next().value); // Giraffe
ES6 introduced numerous powerful features that simplify JavaScript development. Mastering these concepts will significantly enhance your coding efficiency.