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> <h2>JavaScript Getters and Setters</h2> <p>Getters and setters allow you to get and set properties via methods.</p> <p>This example creates a getter for a property called fullName.</p> <p id="demo"></p> <script> // Create an object: var person = { firstName: "John", lastName : "Doe", get fullName() { return this.firstName + " " + this.lastName; } }; // Display data from the object using a getter: document.getElementById("demo").innerHTML = person.fullName; </script> </body> </html>