What are Objects in JavaScript – Objects in JavaScript
Prerequisite – You should have basic knowledge of Javascript and Data type in javascript.
Objects in Javascript
This is only a non-primitive datatype in Javascript. It is very important in javascript because it is a building block of the OOPS concept of javascript and this is only one aspect of OOPS. It is a unique entity that contains properties and methods. Javascript objects are the data types that can takes in collections of key-value pairs.
What! you are asking about what is the property and the method. Hold on hold because we are going to discuss these technical very creative manner in steps.Let’s understand property and method with this real-life example.
Object | Properties | Methods |
![]() | Bike.Name- Steelgrip | Bike.Start() |
Bike.Model-2020 | Bike.Flash() | |
Bike.Weight- 950kg | Bike.Stop() | |
Bike.Color-Blacksketch | Bike.Break() |
Note –
- All bikes have the same properties, but the property value differs from bike to bike.
- All bikes have the same methods, but the methods are performed at different times.
let bike = { name:'steelgrip', model:2020, //method startdisplay : function(){ return (`The name of bike is ${bike.name} and model number is ${bike.model}`) }, //object within object internal_feature : { weight:'950kg', color:'blacksketch' } } console.log(bike.startdisplay()); console.log(bike.internal_feature.weight); //Output will be The name of bike is steelgrip and model number is 2020 950kg
I know you must be thinking what is this weird code all about. So Learner this is our object. An Object can be created with figure brakets{–} with an optional list of properties. A properties is a “key: value” pair, value could be anything as per user wants. And at the same place of key, we can put our method as well which will be our function. So, all these properties you can match up with the above example👆.
50+ HTML, CSS & JavaScript Projects With Source Code
So, we have two-three different approaches to creating objects.
1. Creating Objects in JavaScript
Using object literals: Object literal syntax uses the {…} notation to initialize an object and its methods/properties directly. Let us look at an example of creating objects using this method.
let bike = { name:'steelgrip', model:2020, //method startdisplay : function(){ return (`The name of bike is ${bike.name} and model number is ${bike.model}`) }, //object within object internal_feature : { weight:'950kg', color:'blacksketch' } } console.log(bike.startdisplay()); console.log(bike.internal_feature.weight); //Output will be The name of bike is steelgrip and model number is 2020 950kg
This is the same example as above because we used string literal in this example.
2. Object Constructor: Another way to create objects in JavaScript involves using the “Object” constructor. The Object constructor creates an object wrapper for the given value. This uses new keyword conjugation that allows creating of new objects.
const company = new Object(); company.name = 'steelforth India'; company.location = 'Shrinagar'; company.established = 1951; company.displayInfo = function(){ console.log(`${company.name} was established in ${company.established} at ${company.location}`); } company.displayInfo(); //Output will be steelforth India was established in 1951 at Shrinagar
3. Constructors: Constructors in JavaScript, like in most other OOP languages, provide a template for the creation of objects. In other words, it defines a set of properties and methods that would be common to all objects initialized using the constructor.\
15+ Product Card Using HTML and CSS [ Demo + Code]
Let’s see an example :
function Vehicle(name, maker) { this.name = name; this.maker = maker; } let car1 = new Vehicle('Fiesta', 'Ford'); let car2 = new Vehicle('Santa Fe', 'Hyundai') console.log(car1.name); // Output: Fiesta console.log(car2.name); // Output: Santa Fe
How can we access object members?
- dot notation : (objectName.member name)
const company = new Object(); company.name = 'steelforth India'; company.location = 'Shrinagar'; company.established = 1951; company.displayInfo = function(){ console.log(`${company.name} was established in ${company.established} at ${company.location}`); }
console.log(company.name); // Output: steelforth India console.log(company.established); // Output: 1951
- Bracket Notation : objectName[“memberName”]
const company = new Object(); company.name = 'steelforth India'; company.location = 'Shrinagar'; company.established = 1951; company.displayInfo = function(){ console.log(`${company.name} was established in ${company.established} at ${company.location}`); }
console.log(company.['name']); // Output: steelforth India console.log(company.['established']); // Output: 1951
Iterating over all keys of an object
For iterating over the key of all objects we can use For……in the constructor. Let’s see how its works.
Ecommerce Website Using HTML, CSS, & JavaScript (Source Code)
let employee = { gender : "male" } var worker1 = Object.create(employee); worker1.name = "Newton"; worker1.age = 23; worker1.specialist = "Cricketer"; for (let key in worker1) { console.log(key); } //output name age specialist gender
let person1 = { channelname: "Edux" } // Output : Edux console.log(person1.channelname); delete person1.channelname // Output : undefined console.log(person1.channelname);
By this blog… We have learned Javascript Objects in detail. And we checked all our concepts with performing coding.
What is Object In JavaScript?
Javascript Objects Are The Data Types That Can Takes In Collections Of Key-Value Pairs. It is A unique entity that contains properties and methods