Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

228
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda