JavaScript Functions and Array Methods
JavaScript Functions and Array Methods
A Detailed Guide
Object Inside Array
An array can contain objects, allowing you to store multiple objects with different properties.
Example:
const users = [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 30 },
];
console.log(users[0].name); // Output: Alice
Nested Destructuring
Extracting values from nested objects inside an array using destructuring.
Example:
const users = [
{ name: "Alice", address: { city: "New York", zip: 10001 } },
];
const [{ name, address: { city } }] = users;
console.log(name, city); // Output: Alice New York
Function Declaration
A function defined using the function keyword.
Example:
function greet(name) {
return `Hello, ${name}!`;
}
console.log(greet("Alice"));
Function Expression
A function assigned to a variable.
Example:
const greet = function(name) {
return `Hello, ${name}!`;
};
console.log(greet("Bob"));
Arrow Functions
A concise way to write functions using the => syntax.
Example:
const add = (a, b) => a + b;
console.log(add(3, 4));
Hoisting Introduction
Functions and variables are hoisted to the top of their scope.
Example:
console.log(hello()); // Works due to hoisting
function hello() {
return "Hello!";
}
Example:
function outerFunction() {
function innerFunction() {
return "Inner Function";
}
return innerFunction();
}
console.log(outerFunction());
Lexical Scope
Inner functions have access to outer function variables.
Example:
function outer() {
let x = 10;
function inner() {
console.log(x);
}
inner();
}
outer(); // Output: 10
Example:
{
let a = 10;
var b = 20;
}
console.log(b); // 20
console.log(a); // Error (block-scoped)
Default Parameters
Providing default values to function parameters.
Example:
function greet(name = "Guest") {
return `Hello, ${name}!`;
}
console.log(greet());
Rest Parameters
Using ... to accept multiple arguments as an array.
Example:
function sum(...numbers) {
return numbers.reduce((acc, num) => acc + num, 0);
}
console.log(sum(1, 2, 3));
Parameter Destructuring
Extracting object properties directly in function parameters.
Example:
function printUser({ name, age }) {
console.log(`Name: ${name}, Age: ${age}`);
}
printUser({ name: "Alice", age: 25 });
Callback Functions
Passing a function as an argument.
Example:
function process(num, callback) {
return callback(num);
}
console.log(process(5, (n) => n * 2));
Example:
function outerFunction() {
return function () {
return "Returned function!";
};
}
const inner = outerFunction();
console.log(inner());
forEach Method
Iterates over an array but does not return a new array.
Example:
const arr = [1, 2, 3];
arr.forEach(num => console.log(num * 2));
map Method
Creates a new array with modified elements.
Example:
const numbers = [1, 2, 3];
const squared = numbers.map(num => num * num);
console.log(squared);
filter Method
Returns a new array with elements that match a condition.
Example:
const numbers = [1, 2, 3, 4];
const evens = numbers.filter(num => num % 2 === 0);
console.log(evens);
reduce Method
Reduces an array to a single value.
Example:
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, num) => acc + num, 0);
console.log(sum);
sort Method
Sorts an array (mutates original array).
Example:
const numbers = [4, 2, 5, 1];
numbers.sort((a, b) => a - b);
console.log(numbers);
find Method
Returns the first element that matches a condition.
Example:
const numbers = [1, 2, 3, 4];
const found = numbers.find(num => num > 2);
console.log(found);
every Method
Checks if all elements satisfy a condition.
Example:
const numbers = [2, 4, 6];
console.log(numbers.every(num => num % 2 === 0));
some Method
Checks if at least one element satisfies a condition.
Example:
const numbers = [1, 3, 5, 4];
console.log(numbers.some(num => num % 2 === 0));
fill Method
Fills elements in an array with a static value.
Example:
const arr = [1, 2, 3];
arr.fill(0);
console.log(arr);
splice Method
Adds/removes elements from an array.
Example:
const arr = [1, 2, 3, 4];
arr.splice(1, 2, 99);
console.log(arr);