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

166
Views
Ellipsize middle of string in Javascriptt

I am currently trying to create a function that will ellipsize the middle of a text string. The idea is for it to show the first five characters and the last five characters. The middle of the string will just show as

...

So for example if I have dkjkjfdljfldkjfssdfsf

It should show as

dkjkj...sdfsf

I so far have this function:

function ellipsizeIt(text, length) { if (text.length > length) { return ${text.substring(0, length)}...${text.substring(0, length)}; } return text; }

But its just repeating the string first five characters when I do:

ellipsizeIt(text, 5);

It will show as

dkjkj...dkjkj

Any way to do it so that it doesnt just repeat the first five characters in the end of the string and show the actual last 5 characters?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

From the code above there is a typo/logic issue where you had ${text.substring(0, length)}...${text.substring(0,length)};.

You are repeating yourself, so is the code.

update your code to

function ellipsizeIt(text, length) {
  if (text.length > length) {
    return `${text.substring(0, length)}...${text.substring(text.length -length)}`; 
  }
}

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!