Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript IF statement, detecting null not working

Tags:

javascript

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
like image 799
Fresheyeball Avatar asked Jun 18 '26 15:06

Fresheyeball


2 Answers

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

like image 156
raina77ow Avatar answered Jun 21 '26 05:06

raina77ow


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

like image 21
Charles Avatar answered Jun 21 '26 05:06

Charles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!