menu

JavaScript/Typescript - 16 Topics

DEEP DIVE INTO

JavaScript/Typescript

Topic:object literals

menu

What is object literals?

Object literals is nothing, it's an object with key value pairs, which we already learned in previous chapter in object intro.

Example:

javascriptconst calculation = {
  type: "Addition",
  x : 10, 
  y: 20,
  sum: () => return this.x + this.y; 
}

The value can be a number, a string, an array, a function, or even another object. If the value is a function then it is known as a method. Here greet, x and y is called key or property and sum is called method.

Let's access the value.

javascriptconst typeValue = calculation.type; // "Addition"
const xValue = calculation.x; // 10
const xValue = calculation.y; // 20
const result = calculation.sum(); // 30
1280 x 720 px