How to add an element to a javascript object?

JavascriptObject Oriented ProgrammingProgramming

JavaScript object notation is like

var car = {name:"Benz", "Brand": "Mercedes", model:"open roof"};

 if we need to add the price of the Benz car then the code is as follows.

 Live Demo

Example

<html>
<body>
<p id="demo"></p>
<script>
   var car = {name:"Benz", "Brand": "Mercedes", model:"open roof"};
   car.price = "50,000$",
   car.color = "Black"
   document.write(JSON.stringify(car));
</script>
</body>
</html>

Output

{"name":"Benz","Brand":"Mercedes","model":"open roof","price":"50,000$","color":"Black"}
raja
Published on 21-May-2019 17:29:20
Advertisements