DEV Community

SELVAKUMAR R
SELVAKUMAR R

Posted on

Object in Java Script

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}

Enter fullscreen mode Exit fullscreen mode

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";
Enter fullscreen mode Exit fullscreen mode

Object Access properties:

In Java Script we access the object properties in two ways

  1. Object.Property name
Eg:

console.log(Laptop.brand);
Enter fullscreen mode Exit fullscreen mode

2.Object["Property Name"]

Eg:

Console.log(laptop["storage"]);

Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Output:

Top comments (0)