javascript_methods_and_concepts
javascript_methods_and_concepts
- map():
- Condition: When you need to transform each element in an array (modify, or change) and return a
new array.
- Example:
- filter():
- Condition: When you need to filter elements based on a condition and return a new array with
matching elements.
- Example:
- reduce():
- Condition: When you want to reduce an array to a single value (sum, product, etc.)
- Example:
- forEach():
- Condition: When you want to perform a side effect for each element (without returning a new
array).
- Example:
- find():
- Condition: When you need to find the first element that matches a condition.
- Example:
- some():
- Condition: When you need to check if at least one element meets a condition.
- Example:
- every():
- Example:
- length:
console.log(str.length); // 5
- toLowerCase():
- Example:
console.log(str.toLowerCase()); // "hello"
- toUpperCase():
- Example:
console.log(str.toUpperCase()); // "HELLO"
- includes():
- Example:
console.log(str.includes("world")); // true
- split():
- Condition: When you need to break a string into an array based on a delimiter.
- Example:
- Condition: When you need to replace parts of a string with another string.
- Example:
- trim():
- Condition: When you need to remove extra spaces at the beginning and end of a string.
- Example:
- Destructuring Assignment:
- Condition: When you need to extract values from objects or arrays in a concise way.
- Example:
- Promises:
- Example:
});
fetchData.then(data => console.log(data)); // "Data fetched!"
- Async/Await:
- Example:
console.log(data);
};
- Closures:
- Condition: When a function needs access to variables from its outer scope, even after the outer
- Example:
function outer() {
let count = 0;
count++;
console.log(count);
};
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
- Higher-Order Functions:
- Example: