Welcome to the world of Objects! JavaScript is called an object-oriented programming language, or OOP for short. This means that JavaScript organizes its software design around objects that can contain data or code. Objects are powerful because they allow us to store multiple related values together in a single container.
What are Objects?
Objects simplify storing values by grouping related values into labeled containers. For example, an inventory app for a grocery store would require variables for item names, prices, and stock quantities. Without objects, finding information for specific items becomes difficult as more variables are added. Objects solve this problem by providing a centralized container to store related values.
Object Structure
An object is an unordered collection of properties and values, called key-value pairs, separated by commas inside curly braces. Each key (property name) is linked to a value, which can be of any data type (strings, numbers, booleans, arrays, other objects, or even functions).
Declaring an Object
firstName: 'John',
lastName: 'Doe',
age: 30,
isStudent: false
};
// property names don't need quotes if they would make valid variable names
// the values ('John', 'Doe', 30, false) can be of any type
Pro Tip
If you want a property name that contains spaces or special characters, you must wrap it in quotes. For example: 'first name'
instead of firstName
.