JavaScript Objects

Represent real-world entities using key-value pairs.

propertiesmethodsdestructuring

Table of Contents

JavaScript Objects Tutorial (Beginner -> Practical)

Objects are one of the most important data structures in JavaScript. They allow you to store data in key-value pairs, making them ideal for representing real-world entities like users, products, or settings.

JavaScript objects overview showing key-value pairs, properties, and methods
Overview of JavaScript objects, properties, methods, and practical usage patterns.

1. What is an Object?

An object groups related data and behavior.

let person = {
    name: "John",
    age: 25,
    isStudent: false
};

console.log(person);

Here, name, age, isStudent are keys and their assigned data are values.

2. Accessing Object Properties

Dot Notation

console.log(person.name);
console.log(person.age);

Bracket Notation

console.log(person["name"]);

Bracket notation is useful when the key is dynamic.

3. Modifying Object Properties

person.age = 30;          // update
person.city = "Delhi";    // add new property

console.log(person);

4. Deleting Properties

delete person.isStudent;

5. Object Methods (Functions inside Objects)

let person = {
    name: "John",
    greet: function() {
        console.log("Hello " + this.name);
    }
};

person.greet();

this refers to the current object.

6. Looping Through Objects

let person = {
    name: "John",
    age: 25
};

for (let key in person) {
    console.log(key + ": " + person[key]);
}

7. Nested Objects

Objects inside objects.

let student = {
    name: "Ravi",
    marks: {
        math: 90,
        science: 85
    }
};

console.log(student.marks.math);

8. Object vs Array

FeatureObjectArray
StructureKey-value pairsIndexed list
Accessobj.keyarr[index]
Use CaseReal-world dataCollections

9. Visual Understanding

Objects store data in named key-value format, which makes them readable and useful for entity-style data.

10. Real-Life Example

let product = {
    name: "Laptop",
    price: 50000,
    inStock: true
};

if (product.inStock) {
    console.log(product.name + " is available");
}

11. Important Methods

let person = { name: "John", age: 25 };

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

12. Best Practices

  • Use meaningful property names.
  • Prefer dot notation unless keys are dynamic.
  • Avoid deeply nested objects unless necessary.
  • Keep objects structured and readable.

13. When to Use Objects

Use objects when you need to represent user profiles, product details, API data, or configuration settings.

Related Topics

Arrays, Functions, and DOM Basics rely heavily on object structures in day-to-day JavaScript.

10 Objects Interview Q&A

1Object vs array?easy
Answer: Object uses named keys; array uses numeric indexes.
2Dot vs bracket notation?easy
Answer: Dot for fixed keys, bracket for dynamic keys.
3How to clone object shallowly?easy
Answer: Spread syntax: {...obj}.
4What is this in object method?medium
Answer: Refers to object invoking method (in regular method calls).
5Object.keys returns?easy
Answer: Array of enumerable property names.
6Object.entries returns?easy
Answer: Array of [key, value] pairs.
7Difference between shallow/deep copy?hard
Answer: Shallow copies first level only; deep copies nested structures too.
8How to check key exists?easy
Answer: Use "key" in obj or obj.hasOwnProperty("key").
9Can object have function as value?easy
Answer: Yes, those are object methods.
10When use object destructuring?medium
Answer: To extract fields concisely and improve readability.

10 Objects MCQs

1

Object key access with dynamic key uses:

Adot only
Bbracket notation
Carray index
Dtypeof
Explanation: dynamic key access uses [] notation.
2

Object.keys(obj) returns:

Avalues
Bkeys array
Centries object
Dboolean
Explanation: returns enumerable keys list.
3

Spread clone object syntax:

A{...obj}
B[...obj]
Cobj.clone()
DObject.copy
Explanation: spread in braces creates shallow clone.
4

Method in object is:

Anested array
Bfunction property
Cstring property
Dclass only
Explanation: methods are functions stored in object.
5

for...in iterates:

Aobject keys
Bobject values directly
Conly functions
Dindexes only
Explanation: for...in traverses enumerable keys.
6

Destructuring extracts:

Aall loops
Bproperties into variables
Ccomments
Dclasses
Explanation: object destructuring unpacks fields.
7

How to get key-value pairs array?

AObject.values
BObject.entries
CObject.keys
DObject.pairs
Explanation: entries gives [key,value] pairs.
8

typeof plain object is:

Aarray
Bobject
Cmap
Drecord
Explanation: objects report type "object".
9

Which checks key existence?

A"x" in obj
Bobj.has("x") only
Cobj.exist(x)
Dobj.key(x)
Explanation: in operator checks property existence.
10

Next topic after objects here:

AStrings
BSEO
CBootstrap
DRoadmaps
Explanation: sequence continues to strings.