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

130
Views
How to add text to value in console, without breaking it

so the code bellow would write ten numbers in the console. How could I add some text behind, lets say, number five? So it would write numbers like usual, but the number five would have also some text to it.

for (let x = 1; x < 11; x++) {
console.log(x);
}

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

One more idea is to use switch statement (Could be more readable for a lot of cases "do something on 1" and "2" and "5" and so on).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

<script>
 for (let i = 1; i < 11; i++) {
   switch (i) {
     case 1:
       console.log("hello: " + i);
       break;
     case 5:
       console.log("hello: " + i);
       break;
     default:
       console.log(i);
   }
 }
</script>

default Optional A default clause; if provided, this clause is executed if the value of expression doesn't match any of the case clauses.

** out of topic: it is more common to use i inside for loop (i == index).

about 4 years ago · Santiago Gelvez Report

0

Just use a ternary condition that checks if x is 5.

for (let x = 1; x < 11; x++) {
  console.log(x + (x == 5 ? ' text' : ''));
}

If you want the text before the number you can just move the expression like:

console.log((x == 5 ? 'text ' : '') + x)
about 4 years ago · Santiago Gelvez Report

0

for(let x = 0; x < 11; x++) {
  if(x === 5) {
    console.log('number 5 and additional text');
  } else {
    console.log(x);
  }
}

if I understood your questioncorrectly, this should work

about 4 years ago · Santiago Gelvez 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!