Modern JavaScript ES6+ Features
JavaScript telah berkembang pesat dengan fitur-fitur ES6+. Mari kita explore fitur-fitur modern yang harus Anda kuasai.
Arrow Functions
Arrow functions memberikan syntax yang lebih concise:
// Traditional function
function add(a, b) {
return a + b;
}
// Arrow function
const add = (a, b) => a + b;
Destructuring
Destructuring memudahkan extraction values dari objects dan arrays:
// Object destructuring
const user = { name: 'John', age: 30 };
const { name, age } = user;
// Array destructuring
const [first, second] = [1, 2, 3];
Template Literals
Template literals untuk string interpolation:
const name = 'John';
const greeting = `Hello, ${name}!`;
Async/Await
Async/await membuat asynchronous code lebih readable:
async function fetchData() {
try {
const response = await fetch('/api/data');
const data = await response.json();
return data;
} catch (error) {
console.error('Error:', error);
}
}
Spread Operator
Spread operator untuk copying dan merging:
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5];
const obj1 = { a: 1, b: 2 };
const obj2 = { ...obj1, c: 3 };
Modules
ES6 modules untuk better code organization:
// Export
export const helper = () => {};
export default MyClass;
// Import
import MyClass, { helper } from './module';
Conclusion
Fitur-fitur ES6+ ini essential untuk modern JavaScript development. Practice makes perfect!