A string length which contains one space is always equal to 1:
alert('My str length: ' + str.length);
The space is a character, so:
str = " ";
alert('My str length:' + str.length); // My str length: 3
How can I make a distinction between an empty string and a string which contains only spaces? How can I detect a string which contain only spaces?
To achieve this you can use a Regular Expression to remove all the whitespace in the string. If the length of the resulting string is
0, then you can be sure the original only contained whitespace. Here’s a working example: