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

185
Visualizações
Why `+0n` throws an error but `-0n` does not?

The following code throws an error in javascript:

console.log(String(+0n))

But this code runs successfully:

console.log(String(-0n))

Why +0n throws an error but -0n does not?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

So that it doesn't break asm.js:

  • Unary + followed by an expression is always either a Number, or results in throwing. For this reason, unfortunately, + on a BigInt needs to throw, rather than being symmetrical with + on Number: Otherwise, previously "type-declared" asm.js code would now be polymorphic.

As Bergi highlights in the comments, this was the least bad of three options:

  1. +BigInt -> BigInt: breaks asm.js, and anything else that made the assumption "unary plus gives a Number";
  2. +BigInt -> Number: conflicts with the design decision to disallow implicit conversions between Number and BigInt; or
  3. +BigInt -> error.
about 4 years ago · Juan Pablo Isaza Relatório

0

+0n is treated as +(BigInt(0)), since unary + means "cast to integer", and it can't automatically do that (for some reason)

console.log(+(BigInt(0)));

-0n is treated as BigInt(-0), since negative numbers can be big integers

(You need to check your console for this, since I guess there's a bug in the StackSnippets preventing BigInts from being casted to a string in the console.log call)

console.log(BigInt(-0));

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