![]() The practical layman's answer that solved my head beating on an IF statement string of MULTIPLE chained & each != to a condition was causing FAILURE and needed to legitimately use the single &. With all the lofty, detailed insights throughout, they have mostly missed the truth that there is a conditional evaluation where ONLY the single & will work. These are equivalent: if (user.isLoggedIn()) alert("Hello!")Īlmost all JS compressors use this trick to save 2 bytes. ![]() Therefore, you could use the & operator as a shorter replacement for an if statement. This statement doesn't alert anything and false is returned: true & false & alert("I am quiet!") // returns false This allows Javascript to stop evaluation altogether. True & 20 & 0 & false // returns 0 (it is first false-y)ġ0 & "Rok" & true & 100 // returns 100 (as all are true-y)Īs can be seen from above, as soon as you find one that term is false-y, you needn't to care about the following terms. Here are some examples: true & false & true // returns false Last term otherwise (if all are true-y).However, in JavaScript, it is extended to allow any data type and any number of terms. Most usually, programmers use this operator to check if both conditions are true, for example: true & true // returns true How does it work? Wikipedia has an answer: & is logical AND In case they are not numbers, they are cast to numbers. This operator expects two numbers and retuns a number. In JavaScript, it has questionable performance, and we rarely work with binary data. Other programming languages (like C and Java) use it for performance reasons or to work with binary data. This operator is almost never used in JavaScript.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |