An Object in JavaScript is a collection of properties stored in the form of key-value pairs.
Objects are used to represent real-world entities such as:
- Student
- Employee
- Car
- Product
- Bank Account
Each property of an object consists of:
key : valueExample:
firstname : "John"
age : 30Here,
firstnameis the key."John"is the value.
Objects can be created in three different ways:
- Literal Way
- Constructor Function Way
- Using new Keyword
This is the simplest and most commonly used method for creating objects.
var objectName = {
key1 : value1,
key2 : value2
}var details = {
firstname : "John",
lastname : "Doe",
age : 30
}
console.log(details);
console.log(details.firstname);{ firstname: "John", lastname: "Doe", age: 30 }
John
detailsis an object.firstname,lastname, andageare properties.- We can access properties using:
details.firstnameor
details["firstname"]A function can also be used to create objects.
var fun = function(Wish){
console.log(Wish);
}
fun("Good Morning");Good Morning
funis a function expression."Good Morning"is passed as an argument.- The function prints the value using
console.log().
Note:
This example demonstrates a function.
It is not creating an object yet.
Constructor functions are generally used with the
newkeyword.
The new keyword creates a new object from a constructor function.
var fullname = function(fname, lname){
this.fname = fname;
this.lname = lname;
}
var res = new fullname("John", "Doe");
console.log(res);fullname { fname: 'John', lname: 'Doe' }
this.fname = fname
Stores the first name inside the object.
this.lname = lname
Stores the last name inside the object.
new fullname("John","Doe")
Creates a new object.
Properties can be accessed in two ways.
console.log(res.fname);Output:
John
console.log(res["lname"]);Output:
Doe
Properties can be added after object creation.
Example:
res.age = 25;
console.log(res);Output:
fullname {
fname: "John",
lname: "Doe",
age: 25
}
Example:
res.age = 30;
console.log(res.age);Output:
30
Example:
delete res.age;
console.log(res);Output:
fullname {
fname: "John",
lname: "Doe"
}
Example:
console.log(typeof(res));Output:
object
Common.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Objects in JavaScript</title>
<script src="Objects.js"></script>
</head>
<body>
</body>
</html>Objects.js
var details = {
firstname : "John",
lastname : "Doe",
age : 30
}
console.log(details);
console.log(details.firstname);
var fullname = function(fname, lname){
this.fname = fname;
this.lname = lname;
}
var res = new fullname("John","Doe");
console.log(res);
console.log(res.fname);
console.log(res.lname);-
Objects store data in key-value pairs.
-
Objects are mutable.
-
Properties can be added, modified, and deleted.
-
Objects can contain:
- Strings
- Numbers
- Boolean values
- Arrays
- Functions
- Other Objects
-
typeof(object)returns"object".
Objects are one of the most important features of JavaScript.
They are used to:
- Store related data together.
- Represent real-world entities.
- Build complex applications.
- Work with JSON data.
- Create classes and objects in Object-Oriented Programming.
Understanding objects is essential because JavaScript is an Object-Oriented Programming language, and most modern frameworks such as React, Angular, and Node.js heavily rely on objects.