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

201
Visualizações
Why is this JavaScript function working when it should throw an error?

I use a shorthand function to avoid having to type let x = document.getElementById('myId'); every time I want a reference to an element on the page:

function element(id) {
    return document.getElementById(id);
}

Now I can just use the much simpler:

let x = element("id");

Today, by accident, I did this:

let x = element("id").value;

And it worked, and actually contained the value of the HTML element "id".

But my element() function doesn't provide a .value property. It simply returns a reference to an element.

It should have caused an error of some type. Why didn't it? Or why did it work?

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

0

You've accessed a .value property of a function result, not of a function itself.

element.value // undefined, gets value property of a function
element(id).value // gets an element by id and returns it's value
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