In JavaScript, an object is a complex data structure that allows you to store collections of key-value pairs. Objects are one of the core components of JavaScript and are used to represent real-world entities, such as a user, a car, or any other item that has properties and behaviors.
OR
Object is used to store multiple values. object has key and value pair.
Characteristics of JavaScript Objects
Key-Value Pairs: Each item in an object is stored as a key-value pair, where the key is a string (or Symbol) that identifies the property, and the value can be any data type (string, number, array, function, etc.).
Properties and Methods: The values associated with keys in an object are called properties if they are data and methods if they are functions.
var obj={name:"John",age:35,sex:"Male"}
You can define multiple objects into the Array. you can say these are Array Object.
var users_details=[{name:"John",age:35,sex:"Male"},
{name:"Rom",age:30,sex:"Male"},
{name:"Neena",age:25,sex:"female"}
]
define the method into the Object
var user_details={
first_name:"John",
last_name:"Taylor",
age:35,
full_name: function() {
return this.first_name + " " + this.last_name;
}
}
Note:- full_name is a method in the user_details Object.
get the Object key value
Suppose you have user_details Object and you want to get the key value
var user_details={
first_name:"John",
last_name:"Taylor",
age:35,
full_name: function() {
return this.first_name + " " + this.last_name;
}
}
Example:- get the user’s full_name and age.
console.log(user_details.full_name);
get the age of the user
console.log(user_details.age);
Add key value into the Object
Suppose you have user_details Object
var user_details={
first_name:"John",
last_name:"Taylor",
age:35
}
Now, you want to add user_id into the user_details Object
user_details.user_id=1234
Now get the user_details value
console.log(user_details);
Update key value into the Object
Suppose you have user_details Object
var user_details={
first_name:"John",
last_name:"Taylor",
age:35
}
Now, you have to update age into the user_details Object
user_details.age=36
Now get the user_details value
console.log(user_details);
Delete the key value from the Object
Syntax:-
delete object.key
Example:- delete the age key value from the Object
var user_details={
first_name:"John",
last_name:"Taylor",
age:35
}
delete user_details.age;
console.log(user_details);
Get the Object key value through for in loop
Syntax:- get the Object value
for (var key in Object){
console.log(Object[key]);
}
Example:- Suppose you have users object.
let users={
id:1,
first_name:"John",
last_name:"Taylor",
age:35
}
Get the users object value
for (var key in users){
console.log(users[key]);
}
John
Taylor
35
How to check Object key is exists or not
Syntax:-
object_name.hasOwnProperty(object_key); //Output true or false
Example:- Suppose you have users object.
let users={
id:1,
first_name:"John",
last_name:"Taylor",
age:35
}
Now check the key is exists or not
users.hasOwnProperty("first_name"); //Output true
users.hasOwnProperty("email"); //Output false
How to get the Object’s keys
Syntax:-
Object.keys(object_name);
Example:- Suppose you have users object.
let users={
id:1,
first_name:"John",
last_name:"Taylor",
age:35
}
Now you want the all users object keys
Object.keys(users);
How to get the Object’s values
Syntax:-
Object.values(object_name);
Example:- Suppose you have users object.
let users={
id:1,
first_name:"John",
last_name:"Taylor",
age:35
}
Now you want the all users object values
Object.values(users);