Run ❯
Get your
own
website
Result Size:
625 x 565
×
Change Orientation
Save Code
Change Theme, Dark/Light
Go to Spaces
<!DOCTYPE html> <html> <body> <h1>JavaScript Class Getter/Setter</h1> <p>A demonstration of how to add getters and setters in a class, and how to use the getter to get the property value.</p> <p id="demo"></p> <script> class Car { constructor(brand) { this.carname = brand; } get cnam() { return this.carname; } set cnam(x) { this.carname = x; } } const myCar = new Car("Ford"); document.getElementById("demo").innerHTML = myCar.cnam; </script> </body> </html>