Property Setter



Write a code to illustrate the use of setter method in JavaScript.

<html>
  <head>
    <title>Property Setter</title>
      <script>

        // Create an object:
        var car = {
            color: "Red",
            brand: "Ford",
            set company(value)
            {
                this.brand = value;
            }
        };
        document.write("Company =" + car.brand + <br>);
        car.company="BMW";
        
        // Display data from the object using a getter:
        document.write("Company =" + car.brand);

      </script>
    </head>
  <body>       
</body>
</html>

Output:

Company =Ford
Company =BMW