Somehow, JavaScript makes sense of the bitwise operations NaN ^ 1, Infinity ^ 1 and even 'a' ^ 1 (all evaluate to 1).
What are the rules governing bitwise operators on non numbers? Why do all the examples above evaluate to 1?
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.
According to the ES5 spec, when doing bitwise operations, all operands are converted to
ToInt32(which first callsToNumber. If the value isNaNorInfinity, it’s converted to0).Thus:
NaN ^ 1=>0 XOR 1=>1