最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I distinguish 0 and empty string in javascript? - Stack Overflow

programmeradmin2浏览0评论

Say

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

then

a==b;  // returns true

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

Say

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

then

a==b;  // returns true

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

Share Improve this question asked Jan 28, 2015 at 22:18 Bob SBob S 2952 silver badges12 bronze badges 2
  • 1 here is a really good post of == vs. === stackoverflow./questions/523643/… – taesu Commented Jan 28, 2015 at 22:20
  • possible duplicate of Does it matter which equals operator (== vs ===) I use in JavaScript parisons? – Alexis King Commented Jan 28, 2015 at 22:21
Add a ment  | 

2 Answers 2

Reset to default 8

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

"" === 0; // false

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

发布评论

评论列表(0)

  1. 暂无评论