In Java script object is a Non primitive datatype used to store the collection of related data and complex Entities
Primitive Datatype (strings, numbers, boolen etc) hold only a single value.
Where as Object can hold multiple value as Key-value Pair
Eg:
const Dog = {color: brown, skin:smooth, teeth:medium}
Object can be declared in { } -> Brackets only
Object Constructor:
we using the new Object() method to declare the construct object in Java script
Let Laptop = new Object();
Laptop.brand ="Hp";
Laptop.storage="1 TB HDD";
Object Access properties:
In Java Script we access the object properties in two ways
- Object.Property name
Eg:
console.log(Laptop.brand);
2.Object["Property Name"]
Eg:
Console.log(laptop["storage"]);
Object Java script program:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<!--Object in Javascript-->
<script>
const Laptop=new Object();
Laptop.brand=("Hp");
Laptop.storage=("1 tb Hdd");
console.log(Laptop.brand);
console.log(Laptop["storage"]);
</script>
</body>
</html>
Output:
Top comments (0)