JavaScript Course
JavaScript
/
Beginner

Object Fundamentals

Definition

An object in JavaScript is a collection of related data and/or functionality. These usually consist of several variables and functions, which are called properties and methods when they are inside objects.

Explain Like I'm New

Think of an object like a real-life object, say a Car. A car has properties like color, weight, and model. It also has methods (things it can do) like start, drive, and brake. An object binds these characteristics and actions into one neat package.

Real World Example

In an e-commerce app, a 'Product' is an object. Its properties are title, price, and image. Its methods might include 'addToCart' or 'applyDiscount'.

Common Use Cases

  • •Structuring complex data (like a User profile)
  • •Grouping related functions and variables together
  • •Sending data via JSON to APIs

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • How do you create an object in JavaScript?
  • How do you access properties of an object?

intermediate

  • What is the difference between dot notation and bracket notation?
  • How can you loop through all properties of an object?

advanced

  • What is Object.freeze() vs Object.seal()?
  • How does object destructuring work?

trick

  • Are objects copied by value or by reference?

Flash Cards

Question

What is the difference between dot notation and bracket notation?

Click to reveal answer
Answer

Dot notation (obj.name) is cleaner but requires the property name to be a valid identifier. Bracket notation (obj['name']) allows you to use variables or strings with spaces to access properties.

Question

Are objects copied by value or by reference?

Click to reveal answer
Answer

Objects are copied by reference. If you assign an object to a new variable and modify the new variable, the original object is also modified because they both point to the same memory location.

Question

What is Object.freeze() vs Object.seal()?

Click to reveal answer
Answer

Object.freeze() completely locks the object (no adding, removing, or changing properties). Object.seal() prevents adding or removing properties, but existing properties can still be modified.