0

I am checking the example code. I am not so familiar with Jquery. I never saw this kind of code in JavaScript. He is using Jquery ready function. But inside I can't understand the last line code

timelineComponents['eventsContent'].on('swiperight', function(){
  var mq = checkMQ();
  ( mq == 'mobile' ) && showNewContent(timelineComponents, timelineTotWidth, 'prev');});

Why at the end there is and operation. Is it like If condition? Does it mean that when mq is mobile, then run function showNewContent?

Here is resources code: https://codepen.io/ritz078/pen/LGRWjE

Elias
  • 179
  • 8
  • 1
    Yeah, it's like an `if`, except a lot more confusing to read. (If you have any control over the code, definitely change it to an actual `if` to make it more readable) – CertainPerformance Jan 06 '20 at 09:37
  • 1
    Used in this way `&&` is analogous to the guard operator: https://seanmonstar.com/post/707078771/guard-and-default-operators – Rory McCrossan Jan 06 '20 at 09:38
  • Right. A && B returns the value A if A can be coerced into false; otherwise, it returns B. So here if (mq == 'mobile') is true then only showNewContent will be called. – Yash Sartanpara Jan 06 '20 at 09:39

1 Answers1

0

The statement checks if mq equals the string value 'mobile' and if that statement is true, it will execute the function showNewContent(..)

It's literally the same as

if(mq == 'mobile') {
    showNewContent(timelineComponents, timelineTotWidth, 'prev');
}
BRO_THOM
  • 824
  • 9
  • 24