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

258
Views
setTimeout with prompt causing incrementing default value

Just experimenting with JS. I have found something I cant get my head around.

I have this code:

prompt('title', setTimeout(() => {
    return 'yes'
}, 1000));

When I first run it, it prints 2. But when I run it again, it prints 3.

But what is this counter, and why is it starting from 2? Where does the values come from? Why is it not returning yes?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When you call setTimeout the timeout is given a timeoutID (which can be later used to cancel it with clearTimeout). This timeoutID increments whenever a new timeout is made. When setting it as the default value for the prompt, it is stringified (causing the timeoutID to be returned).

The value starts from two because you've probably made a timeout before running the prompt.

Demonstration:

const first = setTimeout(() => {}, 1).toString()
const second = setTimeout(() => {}, 1).toString()
console.log(first, second)

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!