LearnFrenzy
Sign in Take a free test

Objects, Destructuring and JSON · Lesson 18 of 40

JavaScript Objects: Properties and Methods

4 min read Updated Nov 7, 2026 Saurabh Samir

Objects group related data and behaviour under one name: a user with a name, email and login() method; an order with items, a total and a status. Almost everything in JavaScript is an object or behaves like one – arrays, functions, dates, DOM elements and API responses. Being fluent with objects is essential.

In this lesson

  • Object literals, dot and bracket notation
  • Adding, updating, deleting and checking properties
  • Methods, shorthand syntax and computed keys
  • Looping with Object.keys, values, entries and freezing objects

Creating and reading

const student = {
  name: "Asha Verma",
  age: 21,
  skills: ["JavaScript", "SQL"],
  address: { city: "Pune", pin: "411001" },
  "favourite subject": "DSA",          // keys with spaces need quotes
};
console.log(student.name, student.address.city);
console.log(student["favourite subject"]);

const field = "age";
console.log(student[field]);             // bracket notation with a variable
console.log(student.phone);              // missing property: undefined

Output

Asha Verma Pune
DSA
21
undefined
Use dot notationUse bracket notation
When you know the key while writing code: user.emailWhen the key is in a variable, or has spaces/dashes: user[field], headers["content-type"]

Adding, updating, deleting, checking

const product = { name: "Mouse", price: 599 };
product.price = 549;              // update
product.stock = 20;               // add
delete product.stock;             // remove
console.log(product);

console.log("price" in product, Object.hasOwn(product, "stock"));
console.log("toString" in product, Object.hasOwn(product, "toString"));

Output

{ name: 'Mouse', price: 549 }
true false
true false

in also finds inherited properties (like toString from Object.prototype); Object.hasOwn checks only the object's own keys. Don't check existence with if (obj.count) – it fails when the value is 0 or "".

Methods, shorthand and computed keys

const name = "Ravi", role = "admin";
const key = "last_login";

const user = {
  name,                              // shorthand for name: name
  role,
  [key]: "2026-11-07",               // computed key
  [`is_${role}`]: true,
  greet() {                          // method shorthand
    return `Hi, I'm ${this.name}`;
  },
};
console.log(user);
console.log(user.greet());

Output

{
  name: 'Ravi',
  role: 'admin',
  last_login: '2026-11-07',
  is_admin: true,
  greet: [Function: greet]
}
Hi, I'm Ravi

Inside a method, this refers to the object the method was called on. this has some important rules (and surprises) – chapter 8 covers them fully.

Looping over objects

const marks = { maths: 88, physics: 72, chemistry: 95 };

console.log(Object.keys(marks));
console.log(Object.values(marks));
console.log(Object.entries(marks));

for (const [subject, score] of Object.entries(marks)) {
  if (score >= 80) console.log(`${subject}: distinction`);
}
const total = Object.values(marks).reduce((a, b) => a + b, 0);
console.log("Average:", (total / Object.keys(marks).length).toFixed(1));

Output

[ 'maths', 'physics', 'chemistry' ]
[ 88, 72, 95 ]
[ [ 'maths', 88 ], [ 'physics', 72 ], [ 'chemistry', 95 ] ]
maths: distinction
chemistry: distinction
Average: 85.0

Transform an object with entries

  1. Object.entries(obj)object → array of [key, value] pairs
  2. map / filteruse all the array methods you know
  3. Object.fromEntries(pairs)back to an object
const prices = { pen: 20, book: 250, bag: 899 };
const discounted = Object.fromEntries(
  Object.entries(prices).map(([item, p]) => [item, Math.round(p * 0.9)])
);
const premium = Object.fromEntries(Object.entries(prices).filter(([, p]) => p > 100));
console.log(discounted, premium);

Output

{ pen: 18, book: 225, bag: 809 } { book: 250, bag: 899 }

Key order

Object keys keep insertion order, with one exception: integer-like keys come first, in ascending order.

const o = { b: 1, 2: "two", a: 2, 1: "one" };
console.log(Object.keys(o));

Output

[ '1', '2', 'b', 'a' ]

If you need guaranteed insertion order for any keys (or non-string keys), use a Map (chapter 11).

Freezing objects

const CONFIG = Object.freeze({ apiUrl: "https://api.example.com", retries: 3, flags: { beta: false } });
try {
  CONFIG.retries = 10;
} catch (e) {
  console.log(e.name);           // strict mode: writing to a frozen object throws
}
CONFIG.flags.beta = true;        // freeze is shallow – nested objects can still change
console.log(CONFIG.retries, CONFIG.flags.beta, Object.isFrozen(CONFIG));

Output

TypeError
3 true true

Practice

Try it yourself

  1. Invert an object: { a: "x", b: "y" } → { x: "a", y: "b" }.

    Show solution
    const invert = (obj) => Object.fromEntries(Object.entries(obj).map(([k, v]) => [v, k]));
    console.log(invert({ a: "x", b: "y" }));

    Output

    { x: 'a', y: 'b' }
  2. Find the subject with the highest score in { maths: 88, physics: 72, chemistry: 95 }.

    Show solution
    const marks = { maths: 88, physics: 72, chemistry: 95 };
    const [best] = Object.entries(marks).toSorted((a, b) => b[1] - a[1]);
    console.log(best[0], best[1]);

    Output

    chemistry 95

Quick check

When must you use bracket notation?

When the key is stored in a variable or isn't a valid identifier (spaces, dashes, starts with a digit).

in vs Object.hasOwn?

in includes inherited properties; Object.hasOwn checks only the object's own.

Is Object.freeze deep?

No – nested objects stay mutable unless frozen too.

Summary

  • Objects hold key-value pairs; use dot notation for known keys, brackets for dynamic ones.
  • Shorthand properties, method shorthand and computed keys keep literals concise.
  • Object.keys/values/entries + fromEntries let you use array methods on objects.

Finished this lesson?

Mark it complete to track your progress, then carry on with the next lesson.

Next lesson

Search LearnFrenzy

Esc

Quick links