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

209
Vistas
The difference between "" vs " " Javascript

I used this to remove white spaces from a string.

returnString.split(" ").filter(substr => substr !== "");

In my head it should be this:

returnString.split(" ").filter(substr => substr !== " ")    //note the space between the " "

why doesnt the bottom one work? Is it JS sytax?

Answer: If theres a space at the beginning of the string, its split with an empty string, (so substr !== "") removes that from the returned array when splitting the string.

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

0

returnString.replace(/\s/g,'') Would be a superior way of doing this.

"" is an empty string.

" " is a space character.

They cannot be used interchangeably.

about 4 years ago · Juan Pablo Isaza Denunciar

0

"" is an empty string.

If there is a space at the beginning of the string, it will split that space with an empty string.

that is why substr !== "" is needed to remove the empty string.

about 4 years ago · Juan Pablo Isaza Denunciar

0

As mentioned in the comments, you’re splitting on whitespaces, creating indices for said whitespaces in an array.

const returnString = 'Hello I am Victor'

const str = returnString.split("").filter(substr => substr !== " ");

console.log(str.join(''))

would get you the desired result.

However, a regex pattern would be best suited for this use case.

returnString.replace(/\s/g, '')
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