What’s the difference between | and || in Javascript?
Furthermore, what’s the difference between & and &&?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
|is a bitwise or,||is a logical or.A bitwise or takes the two numbers and compares them on a bit-by-bit basis, producing a new integer which combines the 1 bits from both inputs. So
0101 | 1010would produce1111.A logical or
||checks for the “truthiness” of a value (depends on the type, for integers 0 is false and non-zero is true). It evaluates the statement left to right, and returns the first value which is truthy. So0101 || 1010would return0101which is truthy, therefore the whole statement is said to be true.The same type of logic applies for
&vs&&.0101 & 1010=0000. However0101 && 1010evaluates to1010(&&returns the last truthy value so long as both operands are truthy).