What is an Object Literal?
You'll learn how to create objects using the simplest and most common syntax in JavaScript.
The Core Idea
An object literal is a way to create an object by writing it directly in your code using curly braces {} and listing its properties as key-value pairs. It's called "literal" because you're literally writing out the object right there, just like you'd write a number literal (42) or a string literal ("hello").
Think of it like filling out a form: the keys are the field names, and the values are what you fill in.
The Syntax
const person = {
name: "Alice",
age: 30,
city: "Boston"
};
Here's what's happening:
- Curly braces
{}create the object - Keys (like
name,age,city) are the property names - Colons
:separate keys from their values - Commas
,separate each key-value pair - The whole thing gets stored in the variable
person
Accessing the Properties
Once created, you can read these values:
console.log(person.name); // "Alice"
console.log(person.age); // 30
Why "Literal"?
The term "literal" means you're creating the object inline, right in your code. It's the opposite of constructing an object programmatically or through functions. You're literally typing out its structure.
You can create an empty object literal too:
const emptyObj = {};
Key Takeaway: An object literal uses curly braces {} and key-value pairs separated by colons and commas to create objects directly in your code—it's the most straightforward way to make an object in JavaScript.