0

Weird question regarding cookies in jQuery. I have the following:

console.log("ShowMoveItems() : " + $.cookie('selectedmoveitems'));
console.log("ShowMoveItems() ISNULL : " + $.cookie('selectedmoveitems') === null);

with the following output in Firefox's console:

"ShowMoveItems() : null"
false

For interest's sake, I have also tried == null and it has the same result as ===null. Why is this the case when "ShowMoveItems() : " + $.cookie('selectedmoveitems') shows the cookie as null?

UPDATE afer comment from @RohanKumar:

console.log("ShowMoveItems() : ")
console.log($.cookie('selectedmoveitems'));
console.log("ShowMoveItems() ISNULL : ");
console.log($.cookie('selectedmoveitems') === null);

"ShowMoveItems() : "
null
"ShowMoveItems() ISNULL : "
true
Ebbs
  • 1,030
  • 3
  • 20
  • 38
  • For proper debugging, you should pass stuff to `console.log()` using different arguments, rather than concatenating everything into a string. You should also use parenthesis when composing logical expressions. Last but not least, `$.cookie` is not part of standard jQuery but most likely a plug-in; it never hurts to mention which one. – Álvaro González Jan 22 '14 at 11:55
  • `false`! I think it should be `ShowMoveItems() ISNULL : false` in the second `log message` – Rohan Kumar Jan 22 '14 at 11:57
  • @RohanKumar you are right, the plugin is from [here](https://github.com/carhartl/jquery-cookie). Thanks for the advice regarding regarding console debugging. – Ebbs Jan 22 '14 at 11:59

1 Answers1

0

When casting to string, you get "null" when you have a literal null and a "null" string. There's no way to tell out. Thus I suggest you let the console print variables as-is:

console.log("ShowMoveItems() : ", $.cookie('selectedmoveitems'));

... or (if your console uses a Firebug-compatible API):

console.log("ShowMoveItems() : %o ", $.cookie('selectedmoveitems'));

Also, the === operator has lower precedence than the + (addition) one, thus this:

"ShowMoveItems() ISNULL : " + $.cookie('selectedmoveitems') === null

... equals this:

("ShowMoveItems() ISNULL : " + $.cookie('selectedmoveitems')) === null

... which is obviously false no matter what $.cookie() returns.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • Interesting. This is something I'll definitely remember for future debugging. What you said is showing in the update I posted. This: http://stackoverflow.com/questions/523643/difference-between-and-in-javascript also helps. (It's what I was testing for in the first place) – Ebbs Jan 22 '14 at 12:13