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

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

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