map()
MethodDefinition: map()
creates a new array by transforming every element in an array
using a callback function.
Important Points:
const months = ['January', 'February', 'March'];
const upperMonths = months.map(month => month.toUpperCase());
console.log(upperMonths);
Output: ["JANUARY", "FEBRUARY", "MARCH"]
filter()
MethodDefinition: Returns a new array containing elements that match a given condition.
Important Points:
true
in the callback.
const nums = [1, 2, 3, 4, 5];
const even = nums.filter(num => num % 2 === 0);
console.log(even);
Output: [2, 4]
reduce()
MethodDefinition: Reduces an array to a single value (e.g., sum, product).
Important Points:
accumulator
and current value
.
const nums = [1, 2, 3];
const sum = nums.reduce((acc, val) => acc + val, 0);
console.log(sum);
Output: 6
some()
MethodDefinition: Returns true
if at least one element satisfies the
condition.
const nums = [5, 10, 15];
const hasBig = nums.some(n => n > 12);
console.log(hasBig);
Output: true
every()
MethodDefinition: Returns true
if all elements satisfy the condition.
const nums = [5, 10, 15];
const allBig = nums.every(n => n > 2);
console.log(allBig);
Output: true
arguments
KeywordDefinition: The arguments
object is an array-like object that holds all arguments
passed to a function.
Important Points:
function add() {
let sum = 0;
for (let i = 0; i < arguments.length; i++) {
sum += arguments[i];
}
return sum;
}
console.log(add(10, 20, 30));
Output: 60
Definition: You can set default values for function parameters.
function greet(name = "Guest") {
return "Hello " + name;
}
console.log(greet());
Output: Hello Guest
...
)Definition: Allows iterables (arrays, objects) to be expanded.
const arr1 = [1, 2, 3];
const arr2 = [4, 5];
const combined = [...arr1, ...arr2];
console.log(combined);
Output: [1, 2, 3, 4, 5]
// Spread in objects
const obj1 = { a: 1 };
const obj2 = { b: 2 };
const merged = { ...obj1, ...obj2 };
console.log(merged);
Output: { a: 1, b: 2 }
Definition: Rest parameters allow a function to accept an indefinite number of arguments as an array.
Important Points:
...
, but for gathering arguments.
function sumAll(...numbers) {
return numbers.reduce((total, num) => total + num, 0);
}
console.log(sumAll(5, 10, 15));
Output: 30
Definition: Destructuring allows unpacking values from arrays or properties from objects into distinct variables.
const user = {
name: 'Priyanshu',
age: 15,
address: {
city: 'Dehradun',
state: 'Utrakhand'
}
};
const { name, age } = user;
console.log(name, age); // Output: Priyanshu 15
Output: Priyanshu 15
function intro({ age, name }) {
console.log(age, name);
}
intro(user);
Output: 15 Priyanshu
const colors = ['red', 'green', 'yellow', 'pink', 'black'];
const [firstColor, secondColor] = colors;
console.log(firstColor, secondColor);
Output: red green
map()
: transform elementsfilter()
: select elementsreduce()
: accumulate valuessome()
: at least one matchesevery()
: all must matcharguments
: all passed arguments in a functionDefault Parameters
: define fallback valuesSpread Operator
: combine arrays/objects or expand themRest Parameters
: gather multiple arguments into an arrayDestructuring
: extract values from objects or arrays