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

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

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 Report

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 Report

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