J avaScript Cheat Sheet – Objects &
Arrays
🔶 JavaScript Object Methods
bject.keys(obj): Returns an array of object’s keys
O
Example: Object.keys({a:1, b:2}) // ['a', 'b']
bject.values(obj): Returns an array of object’s values
O
Example: Object.values({a:1, b:2}) // [1, 2]
bject.entries(obj): Returns an array of key-value pairs
O
Example: Object.entries({a:1}) // [['a', 1]]
bject.assign(target, source): Copies properties from source to target
O
Example: Object.assign({}, {a:1}) // {a:1}
bject.freeze(obj): Makes object immutable
O
Example: Object.freeze(obj)
bject.seal(obj): Prevents adding/removing keys but can update values
O
Example: Object.seal(obj)
E xample:
const user = { name: 'Arjun', age: 25 };
console.log(Object.keys(user)); // ['name', 'age']
console.log(Object.values(user)); // ['Arjun', 25]
console.log(Object.entries(user)); // [['name', 'Arjun'], ['age', 25]]
🔷 JavaScript Array Methods
ush(): Adds to end of array
p
Example: arr.push(4)
op(): Removes last element
p
Example: arr.pop()
s hift(): Removes first element
Example: arr.shift()
nshift(): Adds to beginning of array
u
Example: arr.unshift(0)
s plice(start, delete, add): Removes/adds elements
Example: arr.splice(1, 1, 100)
s lice(start, end): Returns a new array from range
Example: arr.slice(1, 3)
c oncat(): Merges arrays
Example: [1, 2].concat([3, 4])
join(): Joins array into string
Example: arr.join('-')
indexOf(): Finds index of element
Example: arr.indexOf(3)
includes(): Checks if array contains value
Example: arr.includes(2)
f orEach(): Iterates over array
Example: arr.forEach(x => console.log(x))
ap(): Transforms array, returns new array
m
Example: arr.map(x => x * 2)
lter(): Returns elements that match condition
fi
Example: arr.filter(x => x > 2)
r educe(): Reduces array to single value
Example: arr.reduce((a, b) => a + b)
nd(): Finds first matching value
fi
Example: arr.find(x => x > 3)
ndIndex(): Index of first matching element
fi
Example: arr.findIndex(x => x === 5)
s ort(): Sorts array
Example: arr.sort()
r everse(): Reverses the array
Example: arr.reverse()
E xample:
const nums = [1, 2, 3, 4];
const doubled = nums.map(x => x * 2); // [2, 4, 6, 8]
const evens = nums.filter(x => x % 2 === 0); // [2, 4]
c onst total = nums.reduce((sum, val) => sum + val, 0); // 10
const firstGreaterThan2 = nums.find(x => x > 2); // 3
🧠 Bonus – Some Useful Tips
heck if object has key:
C
const obj = { name: "Arjun" };
console.log("name" in obj); // true
L oop through object:
for (let key in obj) {
console.log(key, obj[key]);
}
L oop through array:
arr.forEach((val, idx) => {
console.log(idx, val);
});