A handy reference for core JavaScript object and array methods, with examples to quickly recall syntax during interviews or development.
Returns an array of an object's keys.
const obj = { a: 1, b: 2 };
console.log(Object.keys(obj)); // ["a", "b"]Returns an array of an object's values.
console.log(Object.values(obj)); // [1, 2]Returns an array of key-value pairs.
console.log(Object.entries(obj)); // [["a", 1], ["b", 2]]Copies values from one or more objects to a target object.
const target = { a: 1 };
const source = { b: 2 };
const result = Object.assign(target, source);
console.log(result); // { a: 1, b: 2 }Checks if an object has a specific key.
console.log(obj.hasOwnProperty("a")); // trueCreates a new array with results of calling a function on every element.
const nums = [1, 2, 3];
const squared = nums.map(n => n * n);
console.log(squared); // [1, 4, 9]Executes a provided function once for each array element.
nums.forEach(n => console.log(n));Creates a new array with elements that pass a test.
const even = nums.filter(n => n % 2 === 0);
console.log(even); // [2]Executes a reducer function on each element, returning a single output value.
const sum = nums.reduce((total, n) => total + n, 0);
console.log(sum); // 6Returns the first element that passes a test.
const found = nums.find(n => n > 1);
console.log(found); // 2Checks if an array includes a value.
console.log(nums.includes(2)); // trueMerges two or more arrays.
const arr1 = [1, 2];
const arr2 = [3, 4];
const combined = arr1.concat(arr2);
console.log(combined); // [1, 2, 3, 4]Expands or merges arrays/objects.
const merged = [...arr1, ...arr2];
console.log(merged); // [1, 2, 3, 4]Returns a shallow copy of a portion of an array.
const sliced = nums.slice(1);
console.log(sliced); // [2, 3]Changes the contents of an array (remove/replace/add).
nums.splice(1, 1, 10); // Replaces index 1
console.log(nums); // [1, 10, 3]Use this as a quick guide to review core concepts and syntax. Practice with small exercises to memorize!