0

I am currently editing a theme code in which the following javascript snippet occurs:

...
void 0 === Cookies.get("Key") && e("#modal").fadeIn(),
    e("#modal .newsletter-close a").on("click", function (e) {
       e.preventDefault();
    }),
...

First of all, I don't understand the first line.

void boolean && function

Secondly, I don't understand that the functions in the snippet are separated with commas even though these functions are all inside another one and not in an object.

I hope someone can explain what is happening there (not specifically in this snippet, but generally for this spelling) or give me a keyword to google for

4
  • 1
    The void 0 translates to undefined. The && is just an if shorthand. Commented Sep 2, 2021 at 15:33
  • In JS function itself is an object and, therefore, can be passed as a parameter to another function.. Commented Sep 2, 2021 at 15:34
  • That's just a "fancy" way for if (Cookie.get("Key") === undefined) { e(...).fadeIn(); e(...).on(...); } Commented Sep 2, 2021 at 15:37
  • 4
    I'm sure this code was minified and not intended to be read by humans, nobody writes JS like that just for fancyness. Commented Sep 2, 2021 at 15:39

1 Answer 1

1

void forces the return of undefined. This is rarely desirable, however it also forces the line after it to execute, and then throws away the result of that execution. Most often you'll see it used to stop a link from going to it's href. Instead, it'll have something like href="javascript:void(performAction());". In which case, clicking the link will cause the performAction() function to fire and the whole thing returns undefined and the anchor doesn't go anywhere.

The double ampersand is a logical operator, and is saying if the cookie portion is true, and the modal portion is true, then the next thing.

The comma operator acts as a shorthand break between statements.

This is a shorthand way of saying, immediately execute this shorthand if, if the Key cookie is equal to 0 (likely means not set), and the modal is faded in, then attach the click event to the close button of the modal.

More than likely, this modal is dynamically created, and the click event isn't attached when the $(document).ready() function fires, because the modal doesn't exist yet. This is a way to allow the modal to have a click event in the shortest amount of code they could think of.

More here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.