DEV Community

Cover image for Objects Intro
GiandoDev
GiandoDev

Posted on

Objects Intro

What are objects?

Objects are data that contain key-value pairs.
We store our key-value pairs within curly braces. If we want to store more than one key-value pairs, we have to separate each one of them with a comma.
Alt Text
We may think of the objects in Javascript like the objects in the real life, for example the oven and his characteristics:
Alt Text

Object values

Objects may store primitive and object value, therefore every valid value for the JavaScript language.

Getting the value of a property

We may get the value of a property (Key) in two ways:
through object name + a period (".") + property or through object name + square brackets ("[plus "property "]").
Alt Text
There is two circumstances where we have to use square brackets:
first, when the property is a value of a variable:
Alt Text
second, when the property name is an invalid identifier that
respect this rules:
. when it is not one word
. when it starts with a number
. when it is a reserved keyword
. when it doesn`t consist only of numbers, letters or underscores.

Alt Text

Setting the value of a property

We may set the value of a property with the name of our object followed by a dot and then our value.
Or, name of our object followed by square brackets inside double quotes then our value.
Alt Text

Deleting properties

Alt Text

Functions are object in javaScript

Functions are a special kind of object in JavaScript:

  • we may assign a property to a function like an object Alt Text
  • we may assign a function like a value of a property. Alt Text A function that becomes a value of a property is called method and as a normal function we may put inside arguments. We may call our method with dot or square brackets Alt Text

Top comments (1)

Collapse
 
adgower profile image
Alex Gower

Thanks