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

270
Vistas
How do I use a variable (number value) into another value (like pixel value) in javascript?

I am trying to use a JavaScript variable as an HTML pixel value, but I don't know how to use it in a string.

Here's the code:

a = 20;
const h1 = document.querySelector("h1");
h1.style.fontSize = '{a}px';
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use Template literals, which add in a string to the second string.

Here's an example:

const a = 50;
const h1 = document.querySelector("h1");
h1.style.fontSize = `${a}px`;
<h1> Hello </h1>

Or, you can use concatenation for bringing strings together:

a + "px";
// "50px"

Be careful when using concatenation, because if you use a number (like 50) it will add up the string plus the number. You can use a.toString() or String(a) to convert it to a string.

50 + "px"; // "50px"

"50" + "px"; // "50px"

(50).toString() + "px"; // "50px"

String(50) + "px"; // "50px"
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