Open In App

JavaScript | Object Properties

Object properties are defined as a simple association between name and value. All properties have a name and value is one of the attributes linked with the property, which defines the access granted to the property. Properties refer to the collection of values which are associated with the JavaScript object. This collection may not follow any particular order. JavaScript provides the feature to add, delete and modify the properties. Properties are denoted by name:values pairs.

Syntax:



Properties:

Example 1: Shows adding a new property to the existing object.




<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript Object Properties
    </title>
</head>
  
<body>
    <h1>Geeks</h1>
      
    <h3>JavaScript Object Properties</h3>
      
    <p id="gfg"></p>
      
    <!-- Script to add object property -->
    <script>
        var employee = {
            name:"Steve",
            id:"123"
        };
      
        employee.age="25";
          
        document.getElementById("gfg").innerHTML = employee.name
            + " age " + employee.age + " years, and has unique id "
            + employee.id +".";
    </script>
</body>
  
</html>                                

Output:



Example 2: Shows deleting a property from the existing object.




<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript Object Properties
    </title>
</head>
  
<body>
    <h1>Geeks</h1>
      
    <h3>JavaScript Object Properties</h3>
  
    <p id="gfg"></p>
      
    <!-- Script to delete object content -->
    <script>
        var employee = {
            name:"Steve",
            id:"123"
        };
          
        /* Delete employee id */
        delete employee.id;
        document.getElementById("gfg").innerHTML = employee.name
                + " has unique id " + employee.id +"." ;
    </script>
</body>
  
</html>                    

Output:

There are different ways to accessing the object properties. Following examples demonstrate the different accessing techniques:


Article Tags :