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

Javascript IF statement, detecting null not working - Stack Overflow

programmeradmin9浏览0评论

I have a null variable that is behaving weird. For some reason I can't seem to detect whether or not it is null! The var is called emailFromUrl.

   console.log(emailFromUrl); //returns null
   console.log(emailFormUrl.toString()); //returns null
   console.log(emailFromUrl === null); //false!
   console.log(emailFromUrl != null); //true!
   console.log(typeof emailFromUrl); //string
   console.log(!emailFromUrl); //false!
   console.log(emailFromUrl === ""); //false

What the heck is going on here?

The answer:

   console.log(emailFromUrl === 'null'); //true!

The unfiltered console log:

Test71 | emailFromUrl : null | emailFromUrl === null : false | emailFromUrl != null : true | emailFromUrl.toString() : null | typeof emailFromUrl : string | !emailFromUrl : false | emailFromUrl === "" : false | emailFromUrl === "null" : true

I have a null variable that is behaving weird. For some reason I can't seem to detect whether or not it is null! The var is called emailFromUrl.

   console.log(emailFromUrl); //returns null
   console.log(emailFormUrl.toString()); //returns null
   console.log(emailFromUrl === null); //false!
   console.log(emailFromUrl != null); //true!
   console.log(typeof emailFromUrl); //string
   console.log(!emailFromUrl); //false!
   console.log(emailFromUrl === ""); //false

What the heck is going on here?

The answer:

   console.log(emailFromUrl === 'null'); //true!

The unfiltered console log:

Test71 | emailFromUrl : null | emailFromUrl === null : false | emailFromUrl != null : true | emailFromUrl.toString() : null | typeof emailFromUrl : string | !emailFromUrl : false | emailFromUrl === "" : false | emailFromUrl === "null" : true
Share Improve this question edited Aug 14, 2012 at 12:09 Henrik Aasted Sørensen 7,15711 gold badges54 silver badges64 bronze badges asked Jun 8, 2012 at 15:49 FresheyeballFresheyeball 30k21 gold badges105 silver badges167 bronze badges 19
  • What happens if you run this console.log(typeof emailFromUrl); ? – Ben Everard Commented Jun 8, 2012 at 15:51
  • You last log clearly shows it is not equal to null. – Ash Burlaczenko Commented Jun 8, 2012 at 15:51
  • What kind of null are you talking about? Do you mean you've defined it but it's undefined? var emailFromUrl or that it's defined as null var emailFromUrl = '' – PitaJ Commented Jun 8, 2012 at 15:52
  • As far as I can tell, console.log(...) returns undefined. Your string is probably empty. Nothing prints out, it's not null, it is not null, and it has a type of string. Sounds like an empty string. – Ethan Reesor Commented Jun 8, 2012 at 15:54
  • 2 @PitaJ. I got the feeling, if all the lines are right, he's getting "null" instead of null. – gdoron Commented Jun 8, 2012 at 16:04
 |  Show 14 more ments

3 Answers 3

Reset to default 6

Perhaps emailFromurl value is just a literal null string? ) That will explain all the results you get in your question, I think.

well the === operator checks the value and type. emailFromUrl is of type string not null which it why it evaluates to false there.

Unless you want specific logic based on it being null, you can check if the object is "falsy" (if the object is undefined, 0 or null)

if(!emailFromUrl) {
    // handle the null scenario
}
发布评论

评论列表(0)

  1. 暂无评论