Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

&& operator in Javascript

While looking through some code (javascript), I found this piece of code:

<script>window.Bootloader && Bootloader.done(["pQ27\/"]);</script>

What I don't understand is what the && is doing there, the code is from Facebook and is obviously minified and/or obfuscated, but it still does the same thing.

tl;dr: What does the && operator do here?

like image 719
Zar Avatar asked Jan 04 '12 00:01

Zar


2 Answers

&& makes sure that the Bootloader function/object exists before calling the done method on it. The code takes advantage of boolean short circuiting to ensure the first expression evaluates to true before executing the second. See the short-circuit evaluation wikipedia entry for a more in-depth explanation.

like image 146
Sam Dolan Avatar answered Oct 14 '22 05:10

Sam Dolan


window.Bootloader && Bootloader.done(["pQ27\/"]);

it is equivalent to:

if(window.Bootloader) {
  Bootloader.done(["pQ27\/"]);
}
like image 19
ijse Avatar answered Oct 14 '22 07:10

ijse