Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

259
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda