Remove spaces in a Javascript variable
javascript
Solution
This is called string trimming. And here is one option for that in pure JavaScript:
var len = data.replace(/\s/g, "").length;
However, in modern browsers there is a string `trim()` function for that:
var len = data.trim().length;
Problem
I'm using AJAX to retrieve data from MYSQL database through PHP. However, if there is no result found, the variable still has two spaces. I found the problem using `alert(data.length);`. The result is 2, which means there are two spaces. How can I remove these spaces so that if there is no result, I could display a message using `if(data == ''){}`? Thank you!