5

I'm a bit confused when checking if null or undefined and if I should be using !== or != and "undefined" or undefined.

Here is some code I'm working on. Where am I going wrong with my null/unudefined etc?

var c = (jQuery(this).prop("target") != null && jQuery(this).prop("target") != undefined && jQuery(this).prop("target").toLowerCase() == "_blank") ? 1 : 0;

Thanks

K Groll
  • 518
  • 2
  • 8
  • 18

7 Answers7

14

In general, keep it simple.

To check for undefined, use:

foo === undefined
foo !== undefined

To check for null, use:

foo === null
foo !== null

To check for either at the same time, use:

foo == null
foo != null

And in any case, store your .prop() to a variable to keep it clean. But in your case, if it equals "_blank", then you know it isn't null or undefined, so:

var targ = jQuery(this).prop("target").toLowerCase();

var c = targ === "_blank" ? 1 : 0;

Or you could make it even shorter by coercing the boolean to a number:

var targ = jQuery(this).prop("target").toLowerCase();

var c = +(targ === "_blank");

These last two solutions are safe because .prop() will always return a string.

the system
  • 9,244
  • 40
  • 46
  • 1
    @acowley: You're welcome. And don't worry about people that tell you to do funny stuff like `typeof foo === "undefined"`. That's a confusing, bug prone and unnecessary syntax. Just don't ever create a variable named `undefined`, and you'll be ok. – the system Feb 21 '13 at 07:03
  • No Sir. I was thinking of heading down the `typeof` path before asking here. Cheers. – K Groll Feb 21 '13 at 14:39
6
  • Both null and undefined are "falsy" values, thus they can be checked like they were boolean values. Thus, there's no sense comparing to null and undefined except for certain situations where you need to know if they are such values.

  • when comparing, it's best to use strict comparison (like ===,!== and so on)

  • the && in a condition does not evaluate the following condition if the one preceeding it is "falsy".

  • You don't even need jQuery since this is your DOM object (presumably an <a>) and you are trying to get the target property:

In the end:

var c = (this.target && this.target.toLowerCase() === "_blank") ? 1 : 0;
Joseph
  • 117,725
  • 30
  • 181
  • 234
  • 4
    just to clarify, it is not always best to use the 'falsy' properties of null and undefined. This is because values such as zero and empty string are also considered falsy but likely in many instances allowable values. So if you are in a situation were null and undefined values are not allowed, but zero and empty string (and the other falsy values) are allowed, you need to explicitly check for null and undefined. – loesak Aug 27 '13 at 16:13
1

This is the best way to checking undefined:

if(typeof variable_here != 'undefined'){
   // your code here.
 };

And this is the best way to checking null:

if(variable_here !== null){
       // your code here.
     };

So your code should be like this:

var c = (jQuery(this).prop("target") !== null && typeof jQuery(this).prop("target") !== 'undefined' && jQuery(this).prop("target").toLowerCase() == "_blank") ? 1 : 0;
Code.Town
  • 1,216
  • 10
  • 16
0

Since undefined would be the variable type, use typeof

var c = (
 $(this).attr("target") != NULL && 
 typeof $(this).attr("target") != "undefined" && 
 $(this).attr("target").toLowerCase() == "_blank"
) ? 1 : 0;

I think, however, that you only need the last check. Is target "_blank", c needs to be 1, otherwise, 0. Does it really matter if target is even set?

Also, use the attr() methode to get attributes, since prop() if for properties like selectedIndex or tagName.

Lg102
  • 4,733
  • 3
  • 38
  • 61
  • I think the OP is checking the validity of the "target" property so that they can call the `toLowerCase` method on it, otherwise the other two checks are pointless. – Aesthete Feb 21 '13 at 06:37
0

Don't get the same property 3 times just to check a value.

var c = 0;
var prop = $(this).prop("target");
if(prop && prop.toLowerCase() === "_blank") c = 1;
Aesthete
  • 18,622
  • 6
  • 36
  • 45
0

I got something like this which is not relevent to your question but will help you

var targ = jQuery(this).prop("target").toLowerCase();

now if you want to check whether targ is null or undefined

 var c = (!targ || "") ? 1 : 0

hope this will help you

Rohidas Kadam
  • 428
  • 5
  • 12
0

In JavaScript, you are not required to explicitly check if a variable is null or undefined because:

  1. null or undefined return false in a boolean expression.

  2. JS expressions are evaluated from left to right. So for a || b, if a is false, then only b will be evaluated. But if a is true, b will not be checked. Similarly for a && b if a is false, b will not be evaluated.

Hence, if (a != null) { "do something" } can be written as if (a) { "do something" } or simply a && "do something".

In the same way, it can be used to set a default value when the value is set to null or undefined:

function someFunction(age){
    var age= age|| 18;
}

Reference : https://codereview.stackexchange.com/questions/472/usage-of-the-ternary-operator-with-functions-listening-to-click-events

Prince Prasad
  • 1,528
  • 1
  • 16
  • 20