Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

131
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda