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.
Syntax
objectName.propertyName
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'
}
console.log(Student.firstName,'',Student.lastName)
The output of the above code is the following.
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'
console.log(GoT)
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.
Output
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'
console.log(GoT.character)
console.log(GoT.producer)
console.log(GoT.broadcaster)
Output
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'
console.log(GoT.littlefinger)
Here, littlefinger is not attached to the GoT object; the output is undefined.
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'
console.log(GoT['character'])
console.log(GoT['producer'])
console.log(GoT['broadcaster'])
In the code, we have accessed the properties using bracket notation. The output is the following.
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
}
}
console.log(apps.getApp())
Output
That’s it.