JavaScript — Object Definitions
The object literal
The simplest way to create an object is an object literal.
const user = {
name: "Alice",
age: 25
};
This is the form you will use most often for small, direct objects.
Property names
Property names can normally be written without quotes. Strings can also be used explicitly — both are equivalent for normal identifiers:
const user = { name: "Alice", age: 25 };
Special names (spaces, dashes) REQUIRE quotes plus bracket access:
const user = {
"first name": "Alice",
"favorite-color": "blue"
};
user["first name"]; // dot notation will NOT work here
Computed property names
Expressions inside object keys evaluate at creation:
const key = "name";
const user = { [key]: "Alice" }; // user.name === "Alice"
const prefix = "user";
const data = { [`${prefix}Name`]: "Ada" }; // data.userName
Method definitions
const user = {
name: "Alice",
greet: function () { console.log("Hello"); }, // traditional
greet() { console.log("Hello"); } // modern shorthand ✓
};
Prefer the shorthand form.
Nested object definitions
Objects can be defined inside objects — the shape of real-world data:
const company = {
name: "Example",
address: { city: "Ahmedabad", country: "India" }
};
Object constructor syntax
JavaScript also provides new Object():
const user = new Object();
user.name = "Alice";
It works, but literals are clearer. Prefer { }.
Object.create()
Creates an object with a chosen prototype:
const personPrototype = {
greet() { console.log("Hello"); }
};
const user = Object.create(personPrototype);
user.name = "Alice";
user.greet(); // inherited ✓
A null-prototype object makes a clean dictionary:
const data = Object.create(null);
data.name = "Alice";
Object.getPrototypeOf(data); // null
Constructor functions
The pre-class way to stamp out similar objects:
function User(name, age) {
this.name = name;
this.age = age;
}
const alice = new User("Alice", 25);
const bob = new User("Bob", 30);
Uppercase-first names signal "use me with new". With new, JavaScript roughly:
- Creates a fresh object
- Links it to the constructor's prototype
- Binds
thisto it - Runs the constructor
- Returns it
Classes — the modern form
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
const user = new User("Alice", 25);
Same machinery underneath, cleaner syntax on top (full lesson dedicated later).
Factory functions
Return an object instead of needing new:
function createUser(name, age) {
return {
name,
age,
greet() { console.log(`Hello ${this.name}`); }
};
}
const user = createUser("Alice", 25);
user.greet();
Perfectly useful for simple apps; no this-binding surprises for consumers.
Shorthand properties & spread
Same-name variables collapse:
const name = "Alice", age = 25;
const user = { name, age }; // not { name: name, ... }
Spread merges objects — later keys win:
const defaults = { theme: "dark", language: "en" };
const settings = { ...defaults, fontSize: 16 };
const overridden = { ...defaults, theme: "light" }; // theme: "light"
Choosing a definition
| Need | Use |
|---|---|
| One-off data | literal { } |
| Many similar objects with behavior | class or factory |
| Prototype-specific control | Object.create() |
Gotcha:
{}==={}is false (two objects), butb = acreates two references to ONE object.
Mini Practice
- Create a product object literal; add one via
new Object(); rewrite as literal. - Build a constructor function; instantiate three users.
- Convert that constructor into a class.
- Write a factory with a method; call it without
new. - Use computed property names from two variables.
- Merge defaults + overrides with spread; confirm precedence.
Related Topics
Frequently Asked Questions about Object Definitions
What is Object Definitions in JavaScript?
Object Definitions is a fundamental concept in JavaScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Object Definitions?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Object Definitions.
Why is Object Definitions important in JavaScript?
Object Definitions is essential for JavaScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.