JavaScript Objects - What are Objects in JavaScript ?
JavaScript Objects
- A javaScript object is an entity having state and behavior (properties and method). For example: car, pen, bike, chair, glass, keyboard, monitor etc.
- JavaScript is an object-based language. Everything is an object in JavaScript.
- JavaScript is template based not class based. Here, we don't create class to get the object. But, we direct create objects.
- We can able to create objects in javascript and also three methods available.They are
- Object literal
- Creating instance of Object directly (using new keyword)
- Using an object constructor (using new keyword)
JavaScript Object by object literal
- In JavaScript, you can create an object using object literals. An object literal is a comma-separated list of name-value pairs wrapped in curly braces. Here's an example of how to create an object using object literal notation.
Sample Code
Output
By creating instance of Object
- In JavaScript, you can also create an object by creating an instance of the Object constructor function. The Object constructor function is built into JavaScript and can be used to create a new object.
Sample Code
Output
By using an Object constructor
- Here, you need to create function with arguments. Each argument value can be assigned in the current object by using this keyword.