ES6 Features
ES6 (ECMAScript 2015) memperkenalkan fitur-fitur baru dalam JavaScript:
Template Literals
const name = "John";
const age = 25;
// Sebelum ES6
console.log("Nama saya " + name + ", umur " + age);
// Dengan ES6
console.log(`Nama saya ${name}, umur ${age}`);
Destructuring
// Array Destructuring
const [first, second] = [1, 2];
// Object Destructuring
const { name, age } = { name: "John", age: 25 };
Spread Operator
// Array
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5]; // [1, 2, 3, 4, 5]
// Object
const obj1 = { a: 1, b: 2 };
const obj2 = { ...obj1, c: 3 }; // { a: 1, b: 2, c: 3 }
Classes & Modules
// Class
class Person {
constructor(name) {
this.name = name;
}
greet() {
return `Hello, ${this.name}!`;
}
}
// Module
// file: person.js
export class Person {
// ...
}
// file: main.js
import { Person } from './person.js';
Last updated on