Ad space (banner)
🟨JavaScript Lessons
Lesson 7 / 69

Working with Objects

In this lesson you'll learn the basics of using objects in JavaScript so you can group related pieces of information together. This is for beginners searching "JavaScript object usage" or "JavaScript object properties."

An object is a mechanism for grouping data as "name: value" pairs, called properties. You create one with { propertyName: value } and retrieve a value with object.propertyName. If an array is "a list of the same kind of data," an object is well suited to representing "one bundle of information about a single thing" — like a person's profile, bundling related pieces of information together.

The sample code gives an object called user three properties — name, age, and hobby — and retrieves values using dot notation, like user.name. In real web apps, most user information and product information is exchanged in this object shape, and it's very common to combine arrays and objects together.

A common beginner stumbling block is that accessing a property that doesn't exist doesn't throw an error — it just returns undefined. Many people get stuck thinking "I can't get the value" without noticing they made a typo, so double-check your property name spelling carefully. Another common mistake is confusing objects with arrays and trying to access them by number, like user[0].

In real development, JSON data returned from an API can almost always be handled directly as a JavaScript object. Objects appear at the very foundation of web development — for example, bundling a registration form's input values into an object before sending it to a server.

It's extremely common to combine objects and arrays: putting an array inside an object, or lining up objects inside an array. Something like "an array of users, each one an object with name, age, and hobby" is a typical pattern that shows up constantly in real-world data design.

If you want to access an object's property dynamically (using a variable), you use bracket notation, like user["name"]. This shines in situations dot notation can't handle, like when the property name isn't known until runtime.

JavaScript
OUTPUT

💡 Anything passed to console.log() appears in the output below.

Ad space (banner)
Ad space (in-article)