What are the Objects in JavaScript

JavaScript Object is a variable that contains multiple data values. The data values are stored in key-value pairs. Objects can have properties and methods. Properties are attributes of an object, such as name, color, size, etc. Methods are actions an object can perform, such as speaking, moving, calculating, etc.

Object has properties associated with them. A property of the object can be explained as a variable attached to the object. Object properties are ordinary JavaScript variables, except for the attachment to objects.

Objects are composed of attributes. If the attribute contains the function, it is considered the object’s method. Otherwise, the attribute is regarded as property.



Like all JavaScript variables, both the object name (which could be the regular variable) and the property name are case-sensitive. Therefore, you can define the property by assigning it a value.

Object definition

To create an Object in JavaScript, use an object initializer. Using the object initializers is sometimes called creating objects with literal notation. This is because JavaScript has some predefined objects. Also, you can create your objects.

let Student = {
  firstName: 'Krunal',
  lastName: 'Lathiya'


The output of the above code is the following.

Creating new objects

new Object() constructor

Using the Object() constructor, you can use the new keyword and the Object() function to create an empty object and then add properties using dot notation. For example: const person = new Object(); person.name = “Alice”; person.age = 25;

Example 1

let GoT = new Object()

GoT.character = 'Tyrion Lannister'
GoT.producer = 'Dan weiss'
GoT.broadcaster = 'HBO'


In the first line, we created the object called GoT and then assigned the properties to the object.

In the above code, character, producer, and broadcaster are the properties of the GoT object.


Javascript Objects Tutorial Example

Example 2

You can also get the property value one by one using the dot notation like the following.

let GoT = new Object()
GoT.character = 'Tyrion Lannister'
GoT.producer = 'Dan weiss'
GoT.broadcaster = 'HBO'



Working With Objects in JS

If we try to get the property that is not there in the first place, we get undefined in return. See the below example.

let GoT = new Object()

GoT.character = 'Tyrion Lannister'
GoT.producer = 'Dan weiss'
GoT.broadcaster = 'HBO'


Here, littlefinger is not attached to the GoT object; the output is undefined.

Javascript Objects Tutorial

Example 3

Properties of the JavaScript objects can also be accessed or set using bracket notation. See the following example.

let GoT = new Object()

GoT.character = 'Tyrion Lannister'
GoT.producer = 'Dan weiss'
GoT.broadcaster = 'HBO'


In the code, we have accessed the properties using bracket notation. The output is the following.

Objects in JS

Objects are sometimes called an associative array since each property is associated with the string value that can be used to access it.

Object Methods

A method is a function associated with an object or put, and a method is the property of an object that is a function. Methods are defined as a way normal functions are defined, except that they have to be assigned as the property of an object.

let apps = {
  name: 'Facebook',
  getApp: function () {
    return this.name



Defining methods

That’s it.

Recommended Posts

JavaScript Object.keys()

JavaScript Object.values()

JavaScript Object.freeze()

JavaScript Object.assign()

JavaScript Object.create()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.