Please note, this is a STATIC archive of website www.tutorialrepublic.com from September 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Getter and Setter Functions</title> </head> <body> <script> var getValue, setValue; // Self-executing function (function() { var 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>