function: defines a function (a reusable block of code).
if ~ return: if the condition is true, exit the function immediately. return; (with nothing) just stops the function; it doesn’t return a value.
if ~ else: choose between two code paths based on a condition.
Functions
function overlayOn() { if (!overlay) return; // if overlay doesn’t exist, stop overlay.style.display = 'block'; // show the element overlay.classList.add('is-on'); // add CSS class for extra effects }
function overlayOff() { if (!overlay) return; // guard clause overlay.classList.remove('is-on'); // remove the effect class overlay.style.display = 'none'; // hide the element }
!overlay means “overlay is null/undefined”.
return; here only stops the function; it doesn’t “return a result”.
.classList.add/remove/toggle() attaches, removes, or toggles a CSS class.
DOM (Document Object Model): the browser’s object representation of your HTML so JavaScript can read/change it.
Related HTML
<div id="overlay"></div>
It is using an id (#overlay), not a class.
Get Lia’s stories in your inbox
Join Medium for free to get updates from this writer.
Related CSS
#overlay { position: fixed; inset: 0; /* fill the viewport */ z-index: 900; display: none; /* hidden by default */ overflow: hidden; /* If you meant white, prefer #fff or #ffffff. (#ffff is valid 4-digit hex RGBA but uncommon.) */ background: #fff; opacity: 0; /* fully transparent */ transition: opacity .35s ease; }