Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I distinguish 0 and empty string in javascript?

Tags:

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?

like image 916
Bob S Avatar asked Feb 05 '26 20:02

Bob S


2 Answers

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

"" === 0; // false
like image 147
Alexis King Avatar answered Feb 07 '26 09:02

Alexis King


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

like image 30
Raftx24 Avatar answered Feb 07 '26 11:02

Raftx24