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

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

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