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