DEV Community

Nwosa Tochukwu
Nwosa Tochukwu

Posted on

Object in JavaScript

Today I learned that I can create an Object using the Dot (.) notation, the bracket [] notation, and the Coma (,) delimited key-value pairs.

What is actually an Object?
Well from my understanding, It is the grouping of different variables(properties) that descript a character and it helps JavaScript to understand that all variables are related.

  1. Using the Dot (.) notation

The Properties are represented as Key-value pairs where variable names become property key and variable value becomes property value of the object.

example
var dogColor = "Red";
dogColor will be the property key
Red will be the property value

The Syntax for dot (.) notation is

let Car = {}
car.color = "Red";
car.type = "Honda";
Enter fullscreen mode Exit fullscreen mode
  1. The Bracket [] Notation It is an alternative syntax to the Dot notation. The difference is that instead of using dot (.), it uses square brackets [].

The syntax

let car = {}
car["color"] = "Black";
car["type"] = "Honda"
Enter fullscreen mode Exit fullscreen mode

3.** The Coma (,) delimited key-value**
This is listing the key-value pairs inside the Object literal (i.e. the curly braces {}) and it specifies them as coma (,) delimited property because at the end of each key-value will be a coma (,) separating each key-value pair

The Syntax for it

Let car = {
color: "Red",
Type: "Honda",
Enter fullscreen mode Exit fullscreen mode

After each Property comes a colon (:) and follows by its value, after the value is a coma (,).

We can add a new property (Update) and value to the Object by using a dot (.) notation

car.status = "Sold";
car.priceUSD = 1400;
Enter fullscreen mode Exit fullscreen mode

This Syntax of updating the Object works for all types of creating Objects.

Top comments (0)