1. Array Methods
Manipulate and interact with arrays.
push()
Adds elements to the end of an array.
let arr = [1, 2];arr.push(3); // [1, 2, 3]
pop()
Removes the last element from an array.
let arr = [1, 2, 3];arr.pop(); // [1, 2]
map()
Creates a new array by transforming each element.
const doubled = [1, 2, 3].map(num => num * 2); // [2, 4, 6]
filter()
Returns elements that satisfy a condition.
const evens = [1, 2, 3].filter(num => num % 2 === 0); // [2]
slice()
Extracts a portion of an array.
const arr = [1, 2, 3];const sub = arr.slice(1, 3); // [2, 3]
2. String Methods
Work with strings and text.
toUpperCase()
Converts a string to uppercase.
"hello".toUpperCase(); // "HELLO"
split()
Splits a string into an array.
"a,b,c".split(","); // ["a", "b", "c"]
includes()
(ES6)
Checks if a string contains a substring.
"hello".includes("ell"); // true
3. Object Methods
Interact with objects.
Object.keys()
Returns an array of an object’s keys.
const obj = { a: 1, b: 2 };Object.keys(obj); // ["a", "b"]
Object.assign()
(ES6)
Copies properties between objects.
const target = { a: 1 };Object.assign(target, { b: 2 }); // { a: 1, b: 2 }
4. Function Methods
Control function execution context.
bind()
Creates a function with a fixed this
value.
const person = { name: "Alice" };function greet() { console.log(this.name); }const boundGreet = greet.bind(person);boundGreet(); // "Alice"
5. Date Methods
Handle dates and times.
getFullYear()
Returns the year of a date.
new Date().getFullYear(); // 2023
6. ES6+ Features
Modern JavaScript additions.
Arrow Functions
Shorter function syntax.
const add = (a, b) => a + b;add(2, 3); // 5
Destructuring
Unpack values from arrays/objects.
const [x, y] = [1, 2]; // x=1, y=2const { name } = { name: "Bob" }; // name="Bob"
Spread Operator (...
)
Expand arrays/objects.
const arr = [1, 2, ...[3, 4]]; // [1, 2, 3, 4]
7. Promises & Async/Await (ES6/ES8)
Handle asynchronous operations.
Promise
Represents eventual completion.
const fetchData = () => new Promise((resolve) => {setTimeout(() => resolve("Data"), 1000);});
async/await
Write asynchronous code synchronously.
async function getData() {const data = await fetchData();console.log(data); // "Data"}
8. Array Iteration Methods (ES5/ES6)
Modern array utilities.
reduce()
Accumulates values into a single result.
const sum = [1, 2, 3].reduce((acc, num) => acc + num, 0); // 6
find()
(ES6)
Returns the first matching element.
const firstEven = [1, 3, 4].find(num => num % 2 === 0); // 4
No comments:
Post a Comment