Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
How is undefined converted into binary in javascript?

How is undefined treated in Binary in javascript? It throws error when I do (undefined).toString(2).

But when I do

undefined & 0 //returns 0 
undefined & 1 //returns 0 
undefined | 0 //returns 0 
undefined | 1 //returns 1

One might guess that undefined might be converted into 0 . Or Is there any other thing going ?

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

Kind of. When bitwise operators (and other operators which only make sense in terms of math - such as - and * and ** etc) are used, both expressions are converted into numbers first.

4. Let lnum be ? ToNumeric(lval).
5. Let rnum be ? ToNumeric(rval).

And ToNumeric does ToNumber which has

Argument Type   Result
Undefined       Return NaN.

And then the binary operators call ToInt32 on each operator, which has

  1. If number is NaN, +0๐”ฝ, -0๐”ฝ, +โˆž๐”ฝ, or -โˆž๐”ฝ, return +0๐”ฝ.

So they're effectively converted into NaN in preparation for the mathematical operation, and then NaN gets converted to 0 for the binary operation.

(undefined).toString(2) doesn't work because accessing a property (like toString) of an expression only works if the expression is an object, or a primitive that can be wrapped in an object. Undefined and null are not objects, nor can they be converted into one, so trying to access any property of them throws.

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!