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

405
Visualizações
Why doesn't undefined?.fun() throw an error?

I was very surprised to see the following behavior in Node 14.18.0:

> {}?.fun();
Uncaught TypeError: b?.fun is not a function
> undefined?.fun();
undefined

I understand why the first statement throws a TypeError. {}?.fun is undefined, which is not callable. But undefined?.fun is also undefined, so why can it be called without throwing? What part of the ECMAScript specification defines this behavior? Did the ECMAScript working group provide any reasons why it should work this way?

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

0

During optional chaining, if the current value within the chain is null or undefined, the expression short-circuits with a return value of undefined.

This is in the official docs:

The ?. operator is like the . chaining operator, except that instead of causing an error if a reference is nullish (null or undefined), the expression short-circuits with a return value of undefined. When used with function calls, it returns undefined if the given function does not exist.

Since {} is an actual object—without a method called fun()—it blows up, because you called a function that did not exist.

To fix your issue, you will need to call the function with optional chaining:

console.log(({})?.fun?.()); // undefined

about 4 years ago · Juan Pablo Isaza Relatório

0

the work of? and say that possibly the value is undefined

normally used like this in typescript

console.log(myobj?.value ?? 'does not exist');

in javascript "?" it's useless

"?" doesn't do anything, it just signals that the value can be undefined, so when you put an object, it returns an error because this property doesn't exist in your object, whereas in undefined javascript it just ignores everything

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