DEV Community

KISHAN RAMLAKHAN NISHAD
KISHAN RAMLAKHAN NISHAD

Posted on

Objects in JavaScript, along with some interview-style questions and their answers to help you prepare!

What is an Object in JavaScript?
An object is a collection of key-value pairs where:

  • Keys (also called properties) are strings (or Symbols).
  • Values can be any data type: strings, numbers, arrays, functions, other objects, etc.
  1. How Do You Create an Object in JavaScript? Answer:

You can create objects in two main ways:

  1. Using Object Literal Syntax:
const person = {
  name: "Kishan",
  age: 25,
  isDeveloper: true
};

Enter fullscreen mode Exit fullscreen mode

2. How Do You Access and Modify Object Properties?
Answer:

  1. - Dot Notation (when the key is a valid identifier):
console.log(person.name);  // Access: "Kishan"
person.age = 26;           // Modify

Enter fullscreen mode Exit fullscreen mode
  1. Bracket Notation (useful when the key is dynamic or has special characters):
console.log(person["isDeveloper"]);  // Access: true
person["name"] = "Kishan Kumar";     // Modify

Enter fullscreen mode Exit fullscreen mode

**3. How Do You Iterate Over Object Properties?
**Answer:

  1. Using for...in Loop:
for (let key in person) {
  console.log(`${key}: ${person[key]}`);
}

Enter fullscreen mode Exit fullscreen mode

2.Using Object.keys(), Object.values(), or Object.entries():

// Keys
Object.keys(person).forEach(key => {
  console.log(key); // name, age, isDeveloper
});

// Values
Object.values(person).forEach(value => {
  console.log(value); // Kishan, 25, true
});

// Entries (key-value pairs)
Object.entries(person).forEach(([key, value]) => {
  console.log(`${key}: ${value}`);
});

Enter fullscreen mode Exit fullscreen mode

4. What Is the Difference Between Object.keys(), Object.values(), and Object.entries()?
Answer:

  • Object.keys(obj): Returns an array of property names (keys).
  • Object.values(obj): Returns an array of property values.
  • Object.entries(obj): Returns an array of key-value pairs as sub-arrays.
const person = { name: "Kishan", age: 25, isDeveloper: true };

console.log(Object.keys(person));   // ["name", "age", "isDeveloper"]
console.log(Object.values(person)); // ["Kishan", 25, true]
console.log(Object.entries(person)); // [["name", "Kishan"], ["age", 25], ["isDeveloper", true]]

Enter fullscreen mode Exit fullscreen mode

**5. What Are Methods in Objects?
**Answer:

A method is a function stored as a property in an object.

const person = {
  name: "Kishan",
  greet: function() {
    console.log(`Hello, my name is ${this.name}`);
  }
};

person.greet();  // Output: "Hello, my name is Kishan"

Enter fullscreen mode Exit fullscreen mode

*this refers to the current object (person in this case).
*

**6. How Do You Clone or Copy an Object?
**Answer:

  1. Using Object.assign():
const original = { name: "Kishan", age: 25 };
const clone = Object.assign({}, original);

Enter fullscreen mode Exit fullscreen mode
  1. Using Spread Operator (...):
const clone = { ...original };

Enter fullscreen mode Exit fullscreen mode

Note: Both methods perform a shallow copy. For deep cloning, you can use libraries like Lodash or JSON.parse(JSON.stringify(object)) (with limitations).

**7. What is Object Destructuring?
**Answer:

Destructuring allows you to extract properties from an object and assign them to variables.

const person = { name: "Kishan", age: 25, isDeveloper: true };

const { name, age } = person;
console.log(name); // "Kishan"
console.log(age);  // 25

Enter fullscreen mode Exit fullscreen mode

**8. What is the Difference Between == and === When Comparing Objects?
**Answer:

  • == and === both compare references, not values, when dealing with objects.
const obj1 = { name: "Kishan" };
const obj2 = { name: "Kishan" };

console.log(obj1 == obj2);  // false (different references)
console.log(obj1 === obj2); // false (different references)

Enter fullscreen mode Exit fullscreen mode
  • To compare objects by value, you can use JSON.stringify():
console.log(JSON.stringify(obj1) === JSON.stringify(obj2));  // true

Enter fullscreen mode Exit fullscreen mode
  1. What Is Prototypal Inheritance in JavaScript? Answer:

In JavaScript, objects can inherit properties and methods from other objects via prototypes.

Example:

const animal = {
  eats: true
};

const dog = Object.create(animal);
dog.barks = true;

console.log(dog.eats);  // true (inherited from animal)
console.log(dog.barks); // true (own property)

Enter fullscreen mode Exit fullscreen mode

Top comments (0)