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

200
Views
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 answers
Answer question

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 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!