0% found this document useful (0 votes)
5 views

javascript_methods_and_concepts

The document outlines essential JavaScript methods and concepts, including array methods like map, filter, and reduce, as well as string methods such as length and toLowerCase. It also covers advanced topics like destructuring assignment, promises, async/await, closures, the event loop, and higher-order functions. Each method and concept is accompanied by a condition for use and an example for clarity.

Uploaded by

Gaurav Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

javascript_methods_and_concepts

The document outlines essential JavaScript methods and concepts, including array methods like map, filter, and reduce, as well as string methods such as length and toLowerCase. It also covers advanced topics like destructuring assignment, promises, async/await, closures, the event loop, and higher-order functions. Each method and concept is accompanied by a condition for use and an example for clarity.

Uploaded by

Gaurav Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

JavaScript Methods and Concepts

1. JavaScript Array Methods & Conditions

- map():

- Condition: When you need to transform each element in an array (modify, or change) and return a

new array.

- Example:

const numbers = [1, 2, 3];

const doubled = numbers.map(num => num * 2); // [2, 4, 6]

- filter():

- Condition: When you need to filter elements based on a condition and return a new array with

matching elements.

- Example:

const numbers = [1, 2, 3, 4, 5];

const evenNumbers = numbers.filter(num => num % 2 === 0); // [2, 4]

- reduce():

- Condition: When you want to reduce an array to a single value (sum, product, etc.)

- Example:

const numbers = [1, 2, 3, 4];

const sum = numbers.reduce((acc, num) => acc + num, 0); // 10

- forEach():

- Condition: When you want to perform a side effect for each element (without returning a new
array).

- Example:

const numbers = [1, 2, 3];

numbers.forEach(num => console.log(num)); // Logs 1, 2, 3

- find():

- Condition: When you need to find the first element that matches a condition.

- Example:

const numbers = [1, 2, 3, 4];

const found = numbers.find(num => num > 2); // 3

- some():

- Condition: When you need to check if at least one element meets a condition.

- Example:

const numbers = [1, 2, 3, 4];

const hasEven = numbers.some(num => num % 2 === 0); // true

- every():

- Condition: When you need to check if all elements meet a condition.

- Example:

const numbers = [2, 4, 6];

const allEven = numbers.every(num => num % 2 === 0); // true

2. String Methods That Are Most Used

- length:

- Condition: When you need to get the number of characters in a string.


- Example:

const str = "Hello";

console.log(str.length); // 5

- toLowerCase():

- Condition: When you need to convert a string to lowercase.

- Example:

const str = "HELLO";

console.log(str.toLowerCase()); // "hello"

- toUpperCase():

- Condition: When you need to convert a string to uppercase.

- Example:

const str = "hello";

console.log(str.toUpperCase()); // "HELLO"

- includes():

- Condition: When you need to check if a string contains a specific substring.

- Example:

const str = "Hello, world!";

console.log(str.includes("world")); // true

- split():

- Condition: When you need to break a string into an array based on a delimiter.

- Example:

const str = "apple,banana,orange";

const fruits = str.split(","); // ["apple", "banana", "orange"]


- replace():

- Condition: When you need to replace parts of a string with another string.

- Example:

const str = "Hello world";

const newStr = str.replace("world", "everyone"); // "Hello everyone"

- trim():

- Condition: When you need to remove extra spaces at the beginning and end of a string.

- Example:

const str = " Hello world ";

console.log(str.trim()); // "Hello world"

3. Other JavaScript Types That Are Frequently Asked in Interviews

- Destructuring Assignment:

- Condition: When you need to extract values from objects or arrays in a concise way.

- Example:

const person = { name: "John", age: 25 };

const { name, age } = person; // { name: "John", age: 25 }

- Promises:

- Condition: When you need to work with asynchronous operations.

- Example:

const fetchData = new Promise((resolve, reject) => {

setTimeout(() => resolve("Data fetched!"), 2000);

});
fetchData.then(data => console.log(data)); // "Data fetched!"

- Async/Await:

- Condition: When you need to simplify working with Promises.

- Example:

const fetchData = async () => {

const response = await fetch('https://api.example.com');

const data = await response.json();

console.log(data);

};

- Closures:

- Condition: When a function needs access to variables from its outer scope, even after the outer

function has finished executing.

- Example:

function outer() {

let count = 0;

return function inner() {

count++;

console.log(count);

};

const counter = outer();

counter(); // 1

counter(); // 2

- Event Loop:
- Condition: When discussing how JavaScript handles asynchronous operations.

- Example: The event loop ensures that the synchronous code runs first, followed by asynchronous

code (callbacks, promises).

- Higher-Order Functions:

- Condition: When you need to pass functions as arguments or return them.

- Example:

const greet = (name) => `Hello, ${name}!`;

const runFunction = (func, value) => func(value);

console.log(runFunction(greet, "John")); // "Hello, John!"

You might also like