0

I am using a simple code to check if session variable is not set then page redirect to another page usign javascript. My code is:

var userLogin = "<?php echo $_SESSION['user']['id']; ?>";
if (typeof userLogin == "undefined")
{
    $(location).attr('href', 'http://www.example.com/');
}

But it's not working because if session is not set then it assign:

var userLogin = "";

My question is: What is difference between both variable declaration:

var userLogin = "";

and

var userLogin;
Martin C
  • 395
  • 1
  • 7
  • 21

1 Answers1

5

The difference is that var userLogin assigns a value of undefined to userLogin, while var userLogin = "" assigns an empty string as its value.

Use if (!userLogin) instead of if (typeof userLogin == "undefined").


When writing an expression

if (x) { ... }

JavaScript considers the following x values to be false

undefined, null, NaN, 0, "" (empty string), and false

All other values for x are considered true

maček
  • 76,434
  • 37
  • 167
  • 198
Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
  • 1
    perfect short answer +1 – R R Dec 12 '13 at 07:17
  • +1 nice work. You might want to mention which values JavaScript considers `false` – maček Dec 12 '13 at 07:22
  • @maček I thought about it, but decided I don't even want to open that can of worms. For example is `"00.00"` worth mentioning? What about `[0]`? – Dagg Nabbit Dec 12 '13 at 07:37
  • @DaggNabbit, it's actually pretty straightforward. I made an edit for you :) – maček Dec 12 '13 at 08:19
  • @maček I don't think it's that straightforward... the empty array, an array containing a single falsey element, strings like `"0"`, etc. are all falsey. Even their boxed versions, like `new String()`, are falsey. Also, `NaN` is neither truthy nor falsey. It's fair to say that all the values you listed (except NaN) are falsey, but saying that all other values are truthy isn't quite accurate. – Dagg Nabbit Dec 12 '13 at 10:16
  • @maček in fact it's even more complicated than that... I'm considering "falsey" to mean "*x* is falsey if `x == false` evaluates to `true`", but you could also define falsey as "*x* is falsey if `!!x` evaluates to `false`," which would change things for boxed values, arrays, strings representing 0, and NaN (so I guess the definition you are using is more like this). – Dagg Nabbit Dec 12 '13 at 10:22
  • I guess the definition you're using is appropriate when considering `if` statements, but in other contexts it's important to be aware of all the type conversion that goes on under the hood. I've seen people [make this kind of mistake before](http://stackoverflow.com/a/3886106/886931). – Dagg Nabbit Dec 12 '13 at 10:35
  • @DaggNabbit, Right, `!!"0" // true`, `!![0] // true`, and `!!(new String) // true`. As soon as you use `==`, that's where you introduce problems. I don't think I've ever wrote `if (x == false)`, probably because of all the gotchas you're listing. I always use `===` when testing equality, and I always use `!!x` to "cast" as a boolean. – maček Dec 12 '13 at 15:15
  • Either way, I added a little disclaimer around the edit I made :) – maček Dec 12 '13 at 15:18
  • @maček I looked around a bit and your definition of falsey (even though you didn't actually say the word "falsey" I guess that's what we're talking about) seems to be the generally accepted definition... which leaves me wondering what to call things that, when compared to `false` with the abstract equality operator, evaluate to `true`. Maybe there isn't a name for them. The thing is, you can't avoid all the automatic typecasting weirdness just by avoiding `==`. Consider an expression like `"5" % (true + true + true)`. Anyway, sorry for wall-o-comments... I might ask a new question about this. – Dagg Nabbit Dec 12 '13 at 15:48