Short& Sweet — JavaScript Objects

Meghan Elizabeth
2 min readOct 4, 2021

--

Photo by Mockaroon on Unsplash

Object? Properties?

An example of an object would be a cake. A cake has different properties such as flavor and size. All cakes have these properties however, each cakes properties differ. Some cakes could be vanilla flavored with 3 layers, or chocolate flavored with 2 layers.

JavaScript Objects

JavaScript Objects are variables, however, they contain many values. You create /define a JavaScript object with an object literal which is a comma separated list of name value pairs inside curly braces. Spacing doesn’t matter, you can write it like the example below, or it can be written all on one line.

Object Properties

The name-value pairs in JavaScript objects are called properties. In the above example, our properties are flavor: “vanilla”, buttercream: “chocolate” and size: “3-layer”. To break them down even more- our property names are flavor, buttercream and size and our property values are “vanilla”, “chocolate” and “3-layer”.

Accessing Object Properties

To access our object properties we can access them in two different ways:

  1. objectName.propertyName
  2. objectName[“propertyName”]

Using our previous cake example, this would look like the following:

Object Methods

Methods are actions that can be performed on objects and they are stored in properties as function definitions.

Accessing Object Methods

There are different ways you can access an object method:

  1. objectName.methodName()
  2. let name = person.fullName()

Using our cake example this would look like the following:

If you don’t put the () after the method, then it will return the function definition. The () calls the function.

References

--

--

No responses yet