DEV Community

Anjali Gurjar
Anjali Gurjar

Posted on

Array and Object Method of JS

Array Methods in JavaScript
Here’s a list of commonly used array methods with brief explanations and practical interview questions.

Common Array Methods:
push(): Adds elements to the end of the array.
pop(): Removes the last element from the array.
unshift(): Adds elements to the beginning of the array.
shift(): Removes the first element from the array.
map(): Creates a new array by applying a function to each element.
filter(): Creates a new array with elements that pass the test provided by a function.
reduce(): Reduces the array to a single value based on a reducer function.
forEach(): Iterates through each element in the array without returning a value.
find(): Returns the first element that satisfies the provided testing function.
findIndex(): Returns the index of the first element that satisfies the provided testing function.
includes(): Checks if an array contains a certain value.
indexOf(): Returns the index of the first occurrence of a value.
slice(): Returns a shallow copy of a portion of the array.
splice(): Adds/removes elements from the array at specified positions.
concat(): Merges two or more arrays into a new array.
join(): Joins all elements of an array into a string.
reverse(): Reverses the order of the array elements.
sort(): Sorts the array in place.
Array Practical Interview Questions:
Using filter and map: Question: Write a function that takes an array of numbers and returns an array of squares of even numbers.

javascript
Copy code
const numbers = [1, 2, 3, 4, 5, 6];
const evenSquares = numbers.filter(num => num % 2 === 0).map(num => num * num);
console.log(evenSquares); // Output: [4, 16, 36]
Using reduce: Question: Write a function to calculate the sum of all elements in an array.

javascript
Copy code
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
console.log(sum); // Output: 10
Using find: Question: Find the first number in the array greater than 10.

javascript
Copy code
const numbers = [4, 9, 15, 2];
const firstGreater = numbers.find(num => num > 10);
console.log(firstGreater); // Output: 15
Using splice: Question: Remove the 3rd element from the array.

javascript
Copy code
const array = [1, 2, 3, 4, 5];
array.splice(2, 1); // Removes the element at index 2
console.log(array); // Output: [1, 2, 4, 5]
Object Methods in JavaScript
Common Object Methods:
Object.keys(obj): Returns an array of an object's keys.
Object.values(obj): Returns an array of an object's values.
Object.entries(obj): Returns an array of key-value pairs.
Object.assign(target, ...sources): Copies properties from one or more source objects to a target object.
Object.freeze(obj): Freezes an object, preventing additions or modifications.
Object.seal(obj): Seals an object, allowing modifications of existing properties but preventing new properties.
Object.create(proto): Creates a new object with the specified prototype.
Object.is(value1, value2): Compares two values for equality.
Object.fromEntries(entries): Converts key-value pairs into an object.
Object.hasOwnProperty(key): Checks if an object has the specified key as a direct property.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more