DEV Community

Cover image for Objects in javascript.
Ali Sina Yousofi
Ali Sina Yousofi

Posted on

Objects in javascript.

Introduction

In JavaScript, an object is a data structure that contains properties and methods. Properties are key-value pairs that define the characteristics of an object, while methods are functions that are associated with the object.

MDN definition of objects:

JavaScript is designed on a simple object-based paradigm. An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's value can be a function, in which case the property is known as a method.

Objects in JavaScript, just as in many other programming languages, can be compared to objects in real life. In JavaScript, an object is a standalone entity, with properties and type. Compare it with a cup, for example. A cup is an object, with properties. A cup has a color, a design, weight, a material it is made of, etc. The same way, JavaScript objects can have properties, which define their characteristics.

Creating Objects in Javascript.

There are two ways to create an in JavaScript:

1. Object literal notation:

Object literal notation

In this example, person is an object that has four properties: firstName, lastName, age, and fullName. The fullName property is a method that returns the full name of the person. Remember that objects can also contain methods as you see in the above example.

  1. Constructor function:

Constructor function

In this example, Person is a constructor function that creates an object with the same properties and methods as the object created using object literal notation.

Accessing properties

1. Dot Notation

In dot notation first you specify the name of the object and then the name of the property. If that property does not exist it will return undefined since that property is not defined in the object.

Accessing Dot Notation

2. Bracket Notation

In bracket notation you specify name of your object, open a bracket then type name of your property and finally close the bracket.

Bracket Notation

Note

An object property name can be any JavaScript string or symbol, including an empty string. However, you cannot use dot notation to access a property whose name is not a valid JavaScript identifier. For example, a property name that has a space or a hyphen, that starts with a number, or that is held inside a variable can only be accessed using the bracket notation. This notation is also very useful when property names are to be dynamically determined, i.e. not determinable until runtime.

Deleting Properties

1. Using Dot Notation

To delete a property using dot notation, you simply write the object name followed by a dot and the property name, and then apply the delete keyword:

Delete Using Dot Notation

2. Using bracket notation

To delete a property using bracket notation, you enclose the property name in square brackets after the object name, and then apply the delete keyword:

Delete Using bracket notation

Top comments (0)