0

So I used an online JavaScript optimizer and it did something strange to my IF statements that is new to me.

{1===a?window.addEventListener("scroll",turnOff):0===a&&window.removeEventListener("scroll",turnoff)}

What in the world is this? Help me to understand the parts of it please.

AstroCB
  • 11,800
  • 20
  • 54
  • 68
Sebastian
  • 80
  • 9

4 Answers4

1

The three equal signs are a representation of strict comparison operators. Unlike the usual double equal signs, using === compares both the type and value of the expressions being compared. Both the type and value must be equal for the statement to evaluate to true.

The ? and : are shorthand versions of the if statement. When 1===a is true, the statement after the question mark is evaluated. Otherwise, the statement after the colon is evaluated. This shorthand version of the if statement is known as a Conditional (or Ternary) operator.

killQuotes
  • 185
  • 12
  • Thank you, these are all great answers but yours was sorta dumb down for me XD So can you tell me the purpose of &&? why is it 0===a&& and not 0===a? ? – Sebastian Sep 29 '14 at 00:11
  • @Sebastian: Read a beginner's tutorial, and you'll learn all about the basic JavaScript operators. –  Sep 29 '14 at 00:16
  • I did :3 I know its obviously and (and) operator but what is its purpose in this statement. Andddd as I was typing this I just got it.. lmao. But to be on the safe side, its also checks if 0===1 then && is saying also do this if its found true. Otherwise neither statement is used? – Sebastian Sep 29 '14 at 00:21
  • Here's how it evaluates...if "1===a" is false, "0===a" will evaluated and "window.removeEventListener("scroll",turnoff)" will be invoked, which has no return value, so whatever "0===a" returns will be what is returned from the expression. – killQuotes Sep 29 '14 at 00:53
  • thank you, and so I tried running simple code in a test, like b.innerHTML. where b was an id element. it works in the true statement but I could only get functions to work in the second statment. why wouldnt b.innerHTML work in false but it does in true? – Sebastian Sep 30 '14 at 09:16
0

The ? Operator is used just as an if.

if(a){
   b;
} else {
   c;
}

is equal to

a ? b : c;
Grochni
  • 821
  • 9
  • 20
0

It's called a ternary operator. It compares the condition

if a === 1

and returns the first result if the condition is true:

window.addEventListener("scroll",turnOff)

and the second if false:

0===a&&window.removeEventListener("scroll",turnoff)

This line of code does two things, sets a equal to 0 and removes an event listener.

See this tutorial

symlink
  • 10,400
  • 6
  • 25
  • 47
-1

It is a Conditional (Ternary) operator.

You can read more about ternary operators at the developer Mozilla docs:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

It is structured like this:

condition ? code if true : code if false
Mathias Rechtzigel
  • 3,450
  • 1
  • 14
  • 36
kjr1995
  • 395
  • 1
  • 4
  • 9