Vanilla Javascript Method
December 10, 2024Less than 1 minute
Vanilla Javascript Method
map() method
// Simple example with numbers
const numbers = [1, 4, 9];
const doubles = numbers.map(x => x * 2);
console.log(doubles); // Output: [2, 8, 18]
filter() method
// Simple example with numbers
const numbers = [1, 4, 9, 16, 25];
const odds = numbers.filter(x => x % 2!== 0);
console.log(odds); // Output: [1, 9, 16, 25]
reduce() method
// Simple example with numbers
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
console.log(sum); // Output: 15
meaning of ...
// Example with spread operator
const numbers = [1, 2, 3];
const newNumbers = [...numbers, 4, 5];
console.log(newNumbers); // Output: [1, 2, 3, 4, 5]
forEach() method
// Simple example with numbers
const numbers = [1, 4, 9];
numbers.forEach(x => console.log(x * 2)); // Output: 2, 8, 18
bind() method
// Example with bind() method
const greet = function(greeting, name) {
console.log(greeting + " " + name);
}
const sayHello = greet.bind(null, "Hello");
sayHello("John"); // Output: Hello John