TypeScript

Object Properties and Methods in JavaScript and TypeScript - Interview Question

These come up in interviews constantly. Here's a quick reference for the Object methods you'll actually use — and what each one does.

15 Apr 2024

Object Properties and Methods in JavaScript and TypeScript - Interview Question

These come up in interviews constantly. Here's a quick reference for the Object methods you'll actually use — and what each one does.

Reading an object

Object.keys() — returns an array of the object's own property names.

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

Object.values() — returns an array of the object's own property values.

Typescript
const obj = { a: 1, b: 2, c: 3 };
console.log(Object.values(obj)); // [1, 2, 3]

Object.entries() — returns an array of [key, value] pairs. Useful when you need both.

Typescript
const obj = { a: 1, b: 2, c: 3 };
console.log(Object.entries(obj)); // [["a", 1], ["b", 2], ["c", 3]]

Combining and creating objects

Object.assign() — copies properties from source objects to a target. Mutates the target.

Typescript
const target = { a: 1, b: 2 };
const source = { b: 3, c: 4 };
const merged = Object.assign(target, source);
console.log(merged); // { a: 1, b: 3, c: 4 }

Watch out: this is a shallow copy. Nested objects are still references. For deep copies, use structuredClone().

Object.create() — creates a new object with a specific prototype.

Typescript
const person = {
  greet() {
    console.log('Hello!');
  }
};

const john = Object.create(person);
john.greet(); // Hello!

Protecting objects

Object.freeze() — prevents adding, removing, or changing properties. Shallow only.

Typescript
const obj = { a: 1, b: 2 };
Object.freeze(obj);
obj.c = 3; // silently ignored (or throws in strict mode)
console.log(obj); // { a: 1, b: 2 }

hasOwnProperty() — checks if a property belongs directly to the object, not its prototype chain.

Typescript
const obj = { a: 1, b: 2 };
console.log(obj.hasOwnProperty('a')); // true
console.log(obj.hasOwnProperty('toString')); // false

Screenshot 2024-04-15 at 3.33.20 PM.png

The pattern I see in interviews

Interviewers ask about these to gauge whether you understand objects beyond dot notation. Know the difference between own and inherited properties. Know that Object.assign mutates. Know that Object.freeze is shallow. These details separate a confident answer from a shaky one.

Go further
Live cohort on Maven

AI Transformation for Organisational Leaders

For directors, heads of function and founders: eight live evening sessions with Ehsan Gazar and Anoosh Zeighami, ending with a one-page 2027 AI plan for your organisation, built on a real workflow and ready for your leadership team.

$1,495, or $1,045 with code EARLYBIRD until 23 Oct.

View the live cohort

Keep reading