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

262
Vistas
Trying to construct a variable name from a string and another variable name with window[] but not working

This is my code:

function test(tlm) { 
 var soundtimer = 'success';
 var type = tlm.replace('send','');
 var timer = window[type+'timer'];

 console.log(timer);
}

test('sendsound');

I should get success but I get undefined instead. Why is that?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Your 'soundtimer' variable is locally scoped to the test function. Moving it outside allows it to be globally declared on window:

var soundtimer = 'success';
function test(tlm) {
  var type = tlm.replace('send','');
  var timer = window[type+'timer'];

  console.log(timer);
}

test('sendsound');

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use an object within your function.

function test(tlm) { 
  var local = {
    soundtimer:'success'
  };
 var type = tlm.replace('send','');
 var timer = local[type+'timer'];

 console.log(timer);
}

test('sendsound');

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