ES6 Cheatsheet
ES6 Cheatsheet
1. let and const
let: Block-scoped variable declaration.
const: Block-scoped constant declaration.
Example:
let a = 10;
const PI = 3.14;
2. Arrow Functions
Shorter syntax for functions.
Example:
const add = (a, b) => a + b;
3. Template Literals
Multiline strings and string interpolation.
Example:
const name = "John";
const message = `Hello, ${name}!`;
4. Default Parameters
Default values for function parameters.
Page 1
ES6 Cheatsheet
Example:
function greet(name = 'Guest') {
return `Hello, ${name}`;
5. Destructuring Assignment
Extract values from arrays or properties from objects.
Example:
const [a, b] = [1, 2];
const {name, age} = {name: "John", age: 30};
6. Spread and Rest Operators
Spread: Expand array or object.
Rest: Condense remaining arguments into an array.
Example:
const arr = [1, 2, 3];
const newArr = [...arr, 4];
function sum(...nums) {
return nums.reduce((a, b) => a + b);
7. Promises
Handle asynchronous operations.
Page 2
ES6 Cheatsheet
Example:
const promise = new Promise((resolve, reject) => {
if (true) resolve("Success");
else reject("Failure");
});
8. Modules
Export and import code across files.
Example:
// export.js
export const add = (a, b) => a + b;
// import.js
import { add } from './export.js';
9. Classes
Syntax for object-oriented programming.
Example:
class Person {
constructor(name) {
this.name = name;
greet() {
return `Hello, ${this.name}`;
Page 3
ES6 Cheatsheet
10. Enhanced Object Literals
Shorter syntax for object methods and properties.
Example:
const name = "John";
const obj = { name, greet() { return `Hi, ${name}`; } };
11. Map and Set
Map: Key-value pairs.
Set: Unique values.
Example:
const map = new Map();
map.set('key', 'value');
const set = new Set([1, 2, 2, 3]); // {1, 2, 3}
Page 4