Proper term for 'if (object)' as a non-null test?

158 views Asked by At

In ActionScript 3, you can check if a value exists like this:

if (object) {
    trace("This object is not null, undefined, or empty!")
}

I frequently use this as a shorthand for if (object != null)

Is there a proper term for evaluating objects for null in this fashion? I suppose it's a matter of the Boolean typecasting rules for the language but I'm not sure if there's a name for the resulting syntax.

2

There are 2 answers

3
payam_sbr On

Like most computer languages, Ecma-script supports Boolean data types; values which can be set to true or false. In addition, everything in JavaScript has an inherent Boolean value, generally known as either truthy or falsy

list of falsy values (non truthy)

  • false
  • 0 (zero)
  • "" (empty string)
  • null
  • undefined
  • NaN (a special Number value meaning Not-a-Number!)

regularly for checking if a variable is null you may use : (a == null) this statement returns true if a is null, But also return true for all of the above list, because they'r all falsy values and (a == undefined) returns true, even a is not undefined but null or 0 or false.

so you should use Identity operator in this case. following evaluations just returns true when a is null

(typeof a === "null")
// or
(a === null)
4
Vesper On

If your object is always an actual object reference, e.g. a variable of any object type, then checking if (object) is a valid way to test for nulls. If it's a property of variant type, or a dynamic property that can potentially contain simple values (ints, strings etc) then the proper way to test for null will be explicit conversion, probably even with a strict type check if (object !== null).