Bob S
Bob S

Reputation: 295

How can I distinguish 0 and empty string in javascript?

Say

a="";  // empty String
b=0;

then

a==b;  // returns true

What Test could I build to return true only if I compare two empty strings or two zero's?

Upvotes: 2

Views: 1929

Answers (2)

Raftx24
Raftx24

Reputation: 316

use === instead of == for checking undifined and zero or false compare

Upvotes: 1

Alexis King
Alexis King

Reputation: 43842

Use the strict comparison operator, ===. This will not use JavaScript's default type coercion, so you will get the correct result.

"" === 0; // false

Upvotes: 8

Related Questions