Member-only story

A Beginner’s Guide to Object Literals in JavaScript

Understand objects in JavaScript with examples.

Codecupdev
JavaScript in Plain English
5 min readJan 21, 2022

--

What are Objects?

Objects are a data structure in JavaScript like arrays but the difference is that objects are useful when we want to store data that is unordered or where preserving the order of our data does not matter. As an example, if you wanted to store details about a dog, whilst we could achieve this using an array, an object would be more appropriate. This is because we are more interested in the details about the dog as opposed to preserving any order of the details.

Objects also enable us to extend the object to include data about another object. So our dog object could store data about multiple dogs. An object is a container for sets of key-value pairs. Going back to the dog example, if I wanted to store details about a dog called Ted then in the object I may want to store the dog's name, age, and breed. So the name would be the key and the value would be the string ted.

const ted = {
name: "Ted"
};
//name is the key
//"Ted" is the value

Creating Objects

As with arrays objects are reference types so the same rules apply when it comes to using let or const. We can change the object if it was…

--

--

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

No responses yet

Write a response