Object
An object in JavaScript is a standalone data structure that stores data as a collection of key-value pairs, where each key (property name) maps to a specific value. Unlike primitive data types that hold a single value, objects group related data and functionalities together.
An object is a dynamic data structure that stores related data as key-value pairs, where each key uniquely identifies its value.
The values of properties can be primitives, objects, or functions (known as methods when defined inside an object).
Objects are mutable and dynamic properties can be added, modified, or deleted at any time.
Objects allow data grouping and encapsulation, making it easier to manage related information and behaviour together.
There are two primary ways to create an object in JavaScript:
1. Creation Using Object Literal
The object literal syntax allows you to define and initialize an object with curly braces {}, setting properties as key-value pairs.
let obj = {
name: "Sourav",
age: 23,
job: "Developer"
};
console.log(obj);
2. Creation Using new Object() Constructor
let obj = new Object();
obj.name= "Sourav",
obj.age= 23,
obj.job= "Developer"
console.log(obj);
Accessing Object Properties
const person = {
name: "John",
age: 25,
city: "New York"
};
console.log(person);
Dot Notation
console.log(person.name); // John
Bracket Notation
console.log(person["age"]); // 25
Adding Properties
person.email = "john@example.com";
person["country"] = "USA";
Updating Properties
person.age = 26;
Deleting Properties
delete person.city;
Object Methods
An object method is a function stored inside an object.
Example
const student = {
name: "Vicky",
age: 21,
greet: function() {
console.log("Hello");
}
};
student.greet();
Output
Hello
Here name and age are properties, greet() is a method.
Why use methods?
Methods allow an object to perform actions.
const calculator = {
add: function(a, b) {
return a + b;
}
};
console.log(calculator.add(10, 20));
Output
30
Useful Object Methods
Object.keys()
Returns all property names (keys).
const student = {
name: "Vicky",
age: 21
};
console.log(Object.keys(student));
Output
["name", "age"]
Object.values()
Returns all values.
console.log(Object.values(student));
Output
["Vicky", 21]
Object.entries()
Returns both keys and values as arrays.
console.log(Object.entries(student));
Output
[
["name", "Vicky"],
["age", 21]
]
If you want to check whether an object contains a particular property, there are several ways.
1. Using in Operator (Recommended)
const student = {
name: "Vicky",
age: 21
};
console.log("name" in student); // true
console.log("city" in student); // false
Output
true
false
2. Using hasOwnProperty()
const student = {
name: "Vicky",
age: 21
};
console.log(student.hasOwnProperty("name")); // true
console.log(student.hasOwnProperty("city")); // false
Output
true
false
If you want to check whether an object contains a particular value, use Object.values() along with includes().
Example
const student = {
name: "Vicky",
age: 21,
dept: "CSE"
};
console.log(Object.values(student).includes("Vicky")); // true
console.log(Object.values(student).includes("ECE")); // false
Output
true
false
Reference
https://www.geeksforgeeks.org/javascript/objects-in-javascript/
Top comments (0)