JavaScript Object Types
JavaScript objects are mutable collections of properties that can represent structured data and behavior. Arrays, functions, dates, maps, sets, and many other built-in values are objects, although each has its own behavior and APIs.
An object stores properties as key-value associations. Property keys are typically strings or symbols, and values can be primitives, objects, or functions. Objects can be created with object literals such as `{ name: "Ada" }`, and properties can be accessed with dot notation or bracket notation.
Objects are reference values: a variable holds a reference to an object rather than containing an independent copy of the object itself. Assigning one object variable to another therefore makes both variables refer to the same object, so a mutation through either reference is visible through the other.
JavaScript's object category includes more than ordinary object literals. Arrays, functions, `Date` instances, `Map` instances, and `Set` instances are all objects. They provide specialized behavior while still participating in JavaScript's object model.
Objects are mutable by default, meaning existing properties can usually be changed, added, or deleted. `const` prevents reassignment of the variable binding, but it does not by itself prevent mutation of the referenced object.
Objects can inherit properties through their prototype chain. Property lookup first considers the object itself and can then continue through its prototype chain when the requested property is not found.
Key points
- Objects store properties whose values can be primitives or other objects.
- Object variables hold references to objects rather than independent object copies.
- Arrays, functions, dates, maps, and sets are object values.
- `const` prevents reassignment of a binding but does not make an object immutable.
- Objects can inherit properties through a prototype chain.
Interview questions
What is an object in JavaScript?
An object is a collection of properties, where each property has a key and a value. Values can include primitives, objects, or functions. Objects are reference values and are generally mutable. JavaScript also uses objects for specialized types such as arrays and dates.
What happens when you assign one object variable to another?
The second variable receives a reference to the same object. It does not automatically create a new copy. Therefore, changing a property through either variable affects the same underlying object. An independent copy requires an explicit copying strategy.
Why can an object declared with `const` still be changed?
`const` makes the variable binding non-reassignable, but it does not freeze the object referenced by that binding. You can still add, remove, or modify properties unless the object is made immutable through another mechanism. For example, `const user = { name: "Ada" }` allows `user.name = "Grace"`. Reassigning `user` to a different object is what `const` prevents.
Are arrays and functions objects in JavaScript?
Yes. Arrays and functions are objects in JavaScript, so they can have properties and participate in the object model. Arrays additionally provide indexed elements and array methods, while functions are callable and can also have properties. This means `typeof []` and `typeof function(){}` both produce `"object"` and `"function"` respectively, with functions being a special callable object category.
How does JavaScript property lookup work with prototypes?
JavaScript first checks whether the requested property exists directly on the object. If it is not found, lookup can continue through the object's prototype chain. This allows objects to share behavior and properties through prototypes. A property found directly on the object takes precedence over an inherited property with the same key.