a comprehensive overview of JavaScript methods with examples, categorized by their object types and features

 

1. Array Methods

Manipulate and interact with arrays.

push()

Adds elements to the end of an array.

javascript

let arr = [1, 2];
arr.push(3); // [1, 2, 3]

pop()

Removes the last element from an array.

javascript

let arr = [1, 2, 3];
arr.pop(); // [1, 2]

map()

Creates a new array by transforming each element.

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

filter()

Returns elements that satisfy a condition.

javascript

const evens = [1, 2, 3].filter(num => num % 2 === 0); // [2]

slice()

Extracts a portion of an array.

javascript

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.

javascript

"hello".toUpperCase(); // "HELLO"

split()

Splits a string into an array.

javascript

"a,b,c".split(","); // ["a", "b", "c"]

includes() (ES6)

Checks if a string contains a substring.

javascript

"hello".includes("ell"); // true

3. Object Methods

Interact with objects.

Object.keys()

Returns an array of an object’s keys.

javascript

const obj = { a: 1, b: 2 };
Object.keys(obj); // ["a", "b"]

Object.assign() (ES6)

Copies properties between objects.

javascript

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.

javascript

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.

javascript

new Date().getFullYear(); // 2023

6. ES6+ Features

Modern JavaScript additions.

Arrow Functions

Shorter function syntax.

javascript

const add = (a, b) => a + b;
add(2, 3); // 5

Destructuring

Unpack values from arrays/objects.

javascript

const [x, y] = [1, 2]; // x=1, y=2
const { name } = { name: "Bob" }; // name="Bob"

Spread Operator (...)

Expand arrays/objects.

javascript

const arr = [1, 2, ...[3, 4]]; // [1, 2, 3, 4]

7. Promises & Async/Await (ES6/ES8)

Handle asynchronous operations.

Promise

Represents eventual completion.

javascript

const fetchData = () => new Promise((resolve) => {
setTimeout(() => resolve("Data"), 1000);
});

async/await

Write asynchronous code synchronously.

javascript

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.

javascript

const sum = [1, 2, 3].reduce((acc, num) => acc + num, 0); // 6

find() (ES6)

Returns the first matching element.

javascript

const firstEven = [1, 3, 4].find(num => num % 2 === 0); // 4

No comments:

Post a Comment

C# .Net Fundamentals interview questions

  1. Basic .NET Concepts What is the  .NET Framework ? A software framework by Microsoft that provides a runtime (CLR) and libraries for bui...

Best for you