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

168
Vistas
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 Respuestas
Responde la pregunta

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