Member-only story
Source:- What is object in JavaScript
For more questions and answers visit our website at Frontend Interview Questions
In JavaScript, an object is a fundamental data type that represents a collection of key-value pairs. Objects in JavaScript can encapsulate both primitive data types (like strings, numbers, booleans) and other objects, making them powerful and flexible data structures.
An object is defined using curly braces {}, and each key-value pair inside the object is separated by a colon :. The key represents a unique identifier, while the value can be any valid JavaScript data type, including other objects or functions.
Ways to create object
- Simple function
function vehicle(name,maker,engine){
this.name = name;
this.maker = maker;
this.engine = engine;
}
//new keyword to create an object
let car = new vehicle('GT','BMW','1998cc');
//property accessors
console.log(car.name);
2. Creating JS Objects with object literal
//creating js objects with object literal
let car = {
name : 'GT',
maker : 'BMW',
engine : '1998cc'
};
3. Creating object with Object.create() method: