Gain a fundamental understanding of JavaScript objects in this tutorial, where you'll create your own object, learn how to access its properties, and work with the properties of an object, all while understanding the object-oriented nature of JavaScript.
This exercise is excerpted from Noble Desktop’s JavaScript for Front-End training materials and is compatible with JavaScript updates through 2023. To learn current skills in JavaScript with hands-on training, check out our Front-End Web Development Certificate, Full-Stack Web Development Certificate, and coding classes in-person and live online.
Topics covered in this JavaScript tutorial:
Intro to objects, Defining an Object, Accessing & manipulating objects
Exercise Preview
Exercise Overview
In this exercise you’ll learn the fundamentals of JavaScript objects. You’ll create your own object and learn how to access its properties.
JavaScript Objects
JavaScript is an object-oriented language because nearly everything is an object. An object is a collection of properties. Each property is a name (key) with a value, which are often called key-value pairs. If an object’s property value is a function, we’d call it a method.
Objects can store many properties, so they can represent complex, real-world things. For example, a car object might include such properties as doors, color, seats, etc.
Getting Started
- For this exercise we’ll be working with the Objects folder located in Desktop > Class Files > JavaScript Class. Open that folder in your code editor if it allows you to (like Visual Studio Code does).
- In your code editor, open js-objects.html from the Objects folder.
-
Preview js-objects.html in Chrome. (We’ll be using its DevTools.)
This is an almost empty page, but we’ll be focusing on the Console.
Leave this page open in the browser, so you can reload it later.
Defining an Object
- Switch back to your code editor.
-
In the script tag, create a variable where we’ll save our object:
<script> let person = {}; </script>
NOTE: person is the name of our object. You can name an object anything as long as it doesn’t start with a number. The
{}
denotes an object in JavaScript. -
Inside the
{}
for the object, add the following code shown in bold:let person = { name: 'Bob' };
NOTE: person now contains a single property called name that has the string value Bob. Properties can be named anything you like (as long as it starts with a lowercase letter). Properties can also be assigned values of any type (string, number, boolean, etc.). Properties and their values are often referred to as key-value pairs. In our code, name is the key and Bob is the value.
- Save and reload the page in Chrome.
- Open the Console by hitting Cmd–Opt–J (Mac) or Ctrl–Shift–J (Windows).
-
In the Console, type
person
then hit Return (Mac) or Enter (Windows).In the Console you should see
{name: 'Bob'}
Working with Properties of an Object
-
Let’s try using dot syntax to see if we can access the object’s name property. Type the following:
person.name
-
Hit Return (Mac) or Enter (Windows).
In the Console you should see only the value of name:
'
Bob'
- Switch back to your code editor.
-
As shown below, add a key with a number value. Multiple key-value pairs in an object are separated by commas, so be sure not to miss the comma after
'Bob'
:let person = { name: 'Bob', age: 23 };
NOTE: The additional values don’t need to start on a new line but it can help make the code look a little neater and easier to read.
-
Add another key-value pair, this time with a Boolean value:
let person = { name: 'Bob', age: 23, alive: true };
NOTE: A boolean value can only either be true or false.
- Save and reload the page in Chrome.
- The Console should still be open but if you closed it, hit Cmd–Opt–J (Mac) or Ctrl–Shift–J (Windows) to open it.
-
In the Console, type
person
then hit Return (Mac) or Enter (Windows).The Console should show everything in the object (name, age, and alive).
-
Let’s check Bob’s vitals by looking at just the alive value. Type
person.alive
then hit Return (Mac) or Enter (Windows).Phew, it should show the boolean value true.
-
Let’s add another key-value pair dynamically here in the Console. For example, let’s say we want to add a hair color value for Bob. Type:
person.hairColor = 'brown'
NOTE: Remember that any code you write directly to the Console in a browser is only for test purposes and will not be saved in your working document. Make sure not to reload the page, as you will lose your work!
-
Hit Return (Mac) or Enter (Windows) to apply it.
The Console should show
'
brown'
- To check if this change has been added to our object, type
person.hairColor
then hit Return (Mac) or Enter (Windows). - Type
person
then hit Return (Mac) or Enter (Windows) to see all the object’s properties, including the hairColor we just added. -
Bob just had his birthday so we need to change his age. Type the following then hit Return (Mac) or Enter (Windows).
person.age = 24
Type
person
then hit Return (Mac) or Enter (Windows) to see that the age property has been updated to 24. Neat!
JavaScript is Object-Oriented
While we can create our own custom objects (as you learned above), keep in mind that just about everything in JavaScript is an object. JavaScript treats HTML elements as objects. Previously, you’ve seen that you can use getElementById() to grab an element. That element is an object in JavaScript, which has properties that you can get and set.
In Class Files > JavaScript Class > Objects there are two dom PDF files (one is simple and other more detailed).
Those PDF files show a simplified version of the structure of the window object (with just a few example objects).
- Everything is contained inside the topmost node, the window/global object. Think of the window object as the browser window.
- Within the window object, notice the document. This is where all HTML elements live and where you’ll spend most of your time when you’re working in JavaScript.