Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Getter and Setter Functions</title> </head> <body> <script> let getValue, setValue; // Self-executing function (function() { let secret = 0; // Getter function getValue = function() { return secret; }; // Setter function setValue = function(x) { if(typeof x === "number") { secret = x; } }; }()); // Calling the functions document.write(getValue() + "<br>"); // Prints: 0 setValue(10); document.write(getValue() + "<br>"); // Prints: 10 setValue(null); document.write(getValue()); // Prints: 10 </script> </body> </html>