Java Script ES6
Key topics typically covered in such tutorials include:
• Variables: let and const declarations for block-scoped variables.
• Arrow Functions: A more concise syntax for writing functions.
• Classes: Syntactic sugar for creating constructor functions and managing inheritance.
• Template Literals: Enhanced string literals allowing for embedded expressions and multiline strings.
• Destructuring Assignment: A convenient way to extract values from arrays or properties from objects.
• Spread and Rest Operators: For expanding iterables and collecting function arguments.
• Promises: For handling asynchronous operations more effectively.
• Modules: For organizing code into reusable units using import and export.
• Default Parameters: Allowing functions to have default values for parameters.
JavaScript ES6 (ECMAScript 2015) introduced many powerful features that make code cleaner, more concise, and easier to manage. Here's a summary of the most important ES6 features:
1. let and const
• let: block-scoped variable (replaces var)
• const: block-scoped constant (value can’t be reassigned)
2. Arrow Functions
Shorter syntax for writing functions.
js
const add = (a, b) => a + b;
3. Template Literals
Multi-line strings and string interpolation using backticks `.
const name = "Kiran";
console.log(`Hello, ${name}!`);
4. Default Parameters
Set default values for function parameters.
function greet(name = "Guest") {
console.log(`Hello, ${name}`);
}
5. Destructuring
Extract values from arrays or objects.
// Object Destructuring
const person = { name: "Kiran", age: 30 };
const { name, age } = person;
// Array Destructuring
const nums = [1, 2, 3];
const [a, b] = nums;
6. Spread and Rest Operators
• Spread: expand arrays or objects
• Rest: group remaining values
js
const arr1 = [1, 2];
const arr2 = [...arr1, 3, 4]; // Spread
function sum(...numbers) { // Rest
return numbers.reduce((a, b) => a + b);
}
7. Object Literals Enhancements
Shorter syntax for properties and methods.
js
const name = "Kiran";
const person = {
name,
greet() {
console.log("Hi!");
}
};
8. Classes
Simpler syntax for creating objects and inheritance.
js
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a noise`);
}
}
class Dog extends Animal {
speak() {
console.log(`${this.name} barks`);
}
}
9. Promises
For handling asynchronous operations.
const fetchData = () => {
return new Promise((resolve, reject) => {
setTimeout(() => resolve("Data fetched"), 1000);
});
};
fetchData().then(console.log);
10. Modules
Use export and import for modular code.
// math.js
export const add = (a, b) => a + b;
// app.js
import { add } from './math.js';
Comments
Post a Comment