Modern JavaScript Cheat Sheet
Modern JavaScript Cheat Sheet
Table of Contents
▪ Variables
▪ Data Types
▪ Arrays
▪ Array Functions
▪ Objects
▪ Functions
▪ Arrow Functions
▪ Template Literals
▪ Destructuring
▪ Loops
▪ Conditional Statements
▪ Promises
▪ Async/Await
▪ Fetch API
▪ DOM Selection and Manipulation
1. Variables
2. Data Types
// Array, Object
const colors = ['red', 'green', 'blue'];
const person = { name: 'John', age: 30 };
// null, undefined
let value1 = null;
let value2;
// Access element
const firstColor = colors[0]; // 'red'
// Add an element
colors.push('yellow'); // ['red', 'green', 'blue', 'yellow']
4. Array Functions
// Map
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map((num) => num * 2); // [2, 4, 6, 8, 10]
// Filter
const evenNumbers = numbers.filter((num) => num % 2 === 0); // [2, 4]
// Reduce
const sum = numbers.reduce((acc, num) => acc + num, 0); // 15
// forEach
colors.forEach((color) => console.log(color));
// Output:
// 'red'
// 'green'
// 'blue'
5. Objects
const person = {
name: 'John',
age: 30,
};
6. Functions
function greet(name) {
return `Hello, ${name}!`;
}
// Function expression
const add = function (a, b) {
return a + b;
};
// Arrow function
const multiply = (a, b) => a * b;
7. Arrow Functions
8. Template Literals
9. Destructuring
// For Loop
for (let i = 0; i < 5; i++) {
console.log(i);
}
// For...of Loop
const colors = ['red', 'green', 'blue'];
for (const color of colors) {
console.log(color);
}
12. Promises
13. Async/Await
fetch('https://api.example.com/data')
.then((response) => response.json())
.then((data) => console.log(data))
.catch((error) => console.error(error));
Selecting Elements
Manipulating Elements
// Appending elements
parentElement.appendChild(newElement);
// Removing elements
parentElement.removeChild(childElement);
// Event handling
elementById.addEventListener('click', (event) => {
// Handle click event
});