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

104
Vistas
First letter in a sentence dosn’t work in javascript (empty variable in string at a first place)

I use this (I guess typical) function to make a first letter in a sentece capital:

function firstLetteCap(string) {
return string.charAt(0).toUpperCase() + string.slice(1) }

Works perfect till there is an empty variable:

// This works perfect
first = "just"
sentence = `${first} a string`
document.getElementById("p2").innerHTML = `${firstLetteCap(sentence)}.`

// With an empty variable it doesn’t work (capitalized "a" expected)
first = ""
sentence = `${first} a string`
document.getElementById("p3").innerHTML = `${firstLetteCap(sentence)}.`

I would like to know how to fix that, but maybe some context would be also perfect (maybe I don’t get something on how the strings works with variables).

Expected: Just a string.

Thanks!

CodePen: https://codepen.io/vojtacejnek/pen/eYybRqg

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

0

Since, first letter of the sentence variable is (space).

first = ""
sentence = `${first} a string`
document.getElementById("p3").innerHTML = `${firstLetteCap(sentence)}.`

If you want to ignore the extra spaces, you can use .trim().

Refer: https://www.w3schools.com/java/ref_string_trim.asp

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