What are JavaScript Objects?
In JavaScript, almost everything is an object. An object is a collection of related data (properties) and functional logic (methods) that represent a single entity in your application.
javascript
Accessing Properties: Dot vs. Bracket
- Dot Notation (
obj.prop): The most common way to access properties. - Bracket Notation (
obj["prop"]): Essential when the property name is stored in a variable or contains special characters.
javascript
ES6 Features: Destructuring & Shorthand
1. Object Destructuring
Extract multiple properties into individual variables in one line. It makes your code cleaner and easier to read!
javascript
2. Property Shorthand
If your variable name matches the property name, you don't need to specify both.
javascript
Computed Property Keys (ES6)
You can use an expression inside brackets directly within the object literal.
javascript
Essential Object Methods
Built-in static methods for working with objects.
| Method | Purpose | Example |
|---|---|---|
| Object.keys(obj) | Get all keys as an array | ["id", "name"] |
| Object.values(obj) | Get all values as an array | [1, "Alpha"] |
| Object.entries(obj) | Get key-value pairs as an array | [["id", 1], ...] |
| Object.assign(t, s) | Copy properties from source to target | Object.assign({}, old) |
| Object.freeze(obj) | Make object immutable (read-only) | Object.freeze(user) |
javascript
Summary
| Feature | Description |
|---|---|
| Literal | Hand-writing { key: value } |
| Dot Notation | Quick access (obj.prop) |
| Bracket Access | Access with variables (obj[key]) |
| Destructuring | Extract values into variables |
| Spread (...) | Copy or merge objects |
| Freeze | Make object read-only |
| Best For | Modeling entities, passing data, associative maps |
| Method | Function belonging to an object |
| this | Refers to the current object (in methods) |
| Object.entries | Best for looping over key-value pairs |
| keys() | Best for finding all available properties |
| Shallow Copy | Nested objects are not copied, only the reference |
| Seal | Prevents adding/removing, but allows modifying |
| DefineProperty | Control property accessibility (readonly, enumerable) |