JavaScript Objects
In this article you will find information about JavaScript object. The object is explained in very simple language as well as exemple so that you will have easy of understanding.
A JavaScript function is a block of code designed to perform a specific task.
JavaScript provides you with many ways to create an object. The object is most commonly used to use a literal sign.
The JavaScript object is quite different from the primitive data-type number, string, boolean, null, undefined and symbol in the sense that each of these primitive data-types stores a value depending on their types.
JavaScript Object Declaration
- The following example is the syntax for declaring an object:
Example
Edit it yourselfconst cityName = {
city1 : "mumbai",
city2 : "dilhi",
city3 : "ahmedabad"
};
The cityName
is shown as the object as shown in the example.
city1, city1, city1 is the key
to this object.
Mumbai, delhi, ahmedabad is the value of key this object.
Each member of the object has a key:value
pair is separated by a comma and enclosed in spiral brackets { }
.
JavaScript Object Properties
Object properties are defined as a simple connection between a name and a value
All properties have a name and value is one of the attributes attached to the property, defining the access granted to the property.
- The following example shows the structure of the object property.
Example
Edit it yourself<script>
const city = {
name: 'mumbai'
}
city.name = 'mumbai'
city['name'] = 'red'
document.write(car.color);
</script>
In the example above, City is the object and name is the object property.
Mumbai, delhi, ahmedabad is the value of key this object.
Mumbai, delhi, ahmedabad is the value of key this object.
JavaScript Object Methods
JavaScript methods are functions that allow an object to do something or do something for it.
Methods are useful for performing complex mathematical operations on a group of local properties and parameters after the object's content is displayed on the screen.
Different types of methods can be used in JavaScript object method which can refer to any object using methods like call (), apply (), and bind ().
Example
Edit it yourselfconst country = {
state: "28",
pm: "narendra modi",
code: 5566,
bio: function() {
return this.pm;
}
};
document.write(country.bio());
Understand in the example, first create an object named country.
And then in the example state, pm, and code which can be called the property of the object.
And is then called by the function.
- In this lecture you have learned about JavaScript function, hopefully you have understood the function from this lecture.