How does Object inside if condition works?

162 views Asked by At

I have seen people using this

            hideTooltip : function() {
               var toolRef = 'population_tooltip';
               if(Helper.tooltipObj[toolRef]){
                  Helper.tooltipObj[toolRef].hide();
               }
           }

The Helper.tooltipObj[toolRef] is an object. and i was wondering how is it working? can you put an object inside IF condition in Javascript(We are using AUI API)?

The function is called when someone clicks a button on a popup on the page(not the javascript alert type popup, kind of select some stuff and press OK popup)

1

There are 1 answers

0
user2864740 On BEST ANSWER

if (expr) - any expression is valid. If it evaluates to "an object" it is true as all Objects evaluate to a 'true expression' in this context. The expression is considered 'false-y' only for undefined, null, false, "", 0, and NaN.

Thus the condition only runs if "an object" (hopefully with said method) is assigned to the property - it would evaluate to undefined if no value had been previously assigned.

This logic assumes that any other 'true' (or even 'false') value is a prior programming error/contract breach and is a fairly common idiom.


See also: