An object is a type of data in JavaScript. It's the only value you can pass around as variables besides primitives. For this reason, objects are really important in JavaScript.

What are objects



const anObject = {
  key1: 'value1',
  key2: 'value2',
  key3: 'value3'
  // ...


const dictionary = {
  happy: "feeling or showing pleasure or contentment",
  // ...


For example, you can observe the device you're using to read this lesson. What device is this? What's its size? What's its operating system?

If you put these information together into a JavaScript object, it'd look like this:

const macbook = {
  operatingSystem: 'macOS Sierra',
  screenResolution: '2880x1800',
  screenSize: '15.4 inch',
  usbPorts: 2,
  hdmiPort: 1,
  storage: '512gb'
  // ... Other specs

Object values

Objects can contain any value that's valid in JavaScript. This means you can store primitives (like Strings and Numbers) and other objects.

const anObject = {
  string: 'Yay',
  number: 1,
  boolean: true,
  nullPrimitive: null,
  undefinedPrimitive: undefined,
  anotherObject: {},

Getting the value of a property



const prop =

So, if you'd like to get the storage of the macbook we declared above, you can write

const macbookStorage =
console.log(macbookStorage) // 512gb


const macbookStorage = macbook['storage']
console.log(macbookStorage) // 512gb

Both methods work.

Normally, you'll use the dot notation. You'll only use the bracket notation in special occasions where the dot notation doesn't work. These special occasions include:

  1. When your property name is an invalid identifier

Invalid identifiers


  1. The variable must be written as a single word
  3. The variable cannot begin with a number.
  4. The variable cannot be any of these reserved keywords

If your variable follows these four rules, it's a valid identifier; if your variable doesn't follow any of these rules, it's an invalide identifier.

Objects can have invalid identifiers as properties. Here's one example:

const ObjWithInvalidIdentifer = {
  'First Name': 'Zell'

When you have an invalid identifier, you can't use the dot notation. You need to use the bracket notation:

const firstName = ObjWithInvalidIdentifer.First Name // Syntax Error: Unexpected identifier

Of course, when you create objects, try to make properties with valid identifiers so you can use the dot notation. Don't make life hard for yourself!

Getting the value of a property through a variable.


For example, let's say you store the property to search in a variable called propertyToGet.

const propertyToGet = 'storage'

There's no property called propertyToGet in the object, so you can't use the dot notation. If you try doing it, you'll get undefined.

const storageWithDotNotation = macbook.propertyToGet
console.log(storageWithDotNotation) // undefined

You can only use the bracket notation, like this:

const macbookStorage = macbook[propertyToGet]
console.log(macbookStorage) // 512gb

That's all you need to know about getting property values. Let's move on and learn to set property values.

Setting the value of a property


// Dot notation = '256gb'

// Bracket notation
macbook['usbPorts'] = 2

// {
//   storage: '256gb',
//   usbPorts: 2
// }

Deleting properties



Here's an example where we delete the storage property from the macbook Object.

// {
//   usbPorts: 2
// }

Functions are Objects in JavaScript

Functions are a special kind of Object in JavaScript. It can have properties too (even though you'd most likely won't declare any).

// You can add properties to functions
function sayName () {} = 'Hallelujah'

// Hallelujah


const anObject = {
  aMethod: function() {
    // Do something in function


const macbook = {
  playMusic: function () {
    /* some code to play music */

Since methods are functions, they behave exactly like functions.


// Calling a method with the dot notation

// Calling a method with the bracket notation


const greeter = {
  sayHello: function (name) {
    console.log('Hello ' + name + '!')

// Hello Zell!

You may hear phrases like *"higher order functions"* and *"functions are first class objects"* when you browse outside tutorials. Ignore these phrases. They both mean functions are objects, which means you can pass functions around as a variable. People like to introduce important-sounding jargons to make functions sound more amazing (and confusing) than they really are.

Exercises to help you get better with objects


  1. Make an empty object
  4. Get the value of a property with the dot notation
  6. Set the value of a property with the dot notation
  8. Make a method. Call this method