What is object in JS explain with example?

0 votes
24 views
Ravi Vishwakarma asked 10 days ago in Web Application by Ravi Vishwakarma

What is object in JS explain with example?    

1 Answer

1 votes
Ethan Karla answered 9 days ago by Ethan Karla
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 are create object by 3 way
1. By object literal
2. By creating instance of object directly (using new keyword)
3. By using an object constructor (using new keyword)
By object literal
Syntax:
 Object-name = {property1:value1, property2:value2 --- propertyN:valueN } ;
Example:
emp={id:102,name:'Shyam Kumar',salary:40000}  

console.log(emp.id+' '+emp.name+' '+emp.salary);
By creating instance of object directly (using new keyword)
Syntax:
 var objectname=new Object();  
Example:
var emp=new Object();  

emp.id=101;
emp.name='Ravi Malik';
emp.salary=50000;
console.log(emp.id+' '+emp.name+' '+emp.salary);
By using an object constructor (using new keyword)
Syntax:
 function emp(arguments1, arguments2,---){  

 this.property1=argument1;
 this.property2=argument2;
 this.propertyN=argumentN;
 }
Example:
function emp(id,name,salary){  

this.id=id;
this.name=name;
this.salary=salary;
}
e=new emp(103,'Vimal Jaiswal',30000);
console.log(e.id+' '+e.name+' '+e.salary);