Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "!" operator mean in javascript when it is used with a non-boolean variable?

While reading through javascript codes I've been seeing the ! operator used for non boolean variables. Here is an example of code not used in.

/**
 * loads a resource from a url
 * @param {string} url the url of the resource to load
 * @param {string} relativeTo the url to load relative to
 * @param {function} callback thefunction to call once the file is loaded
 * @private
 */
 GLGE.Wavefront.prototype.loadFile=function(url,relativeTo,callback){
    if(this.relativeTo && !relativeTo) relativeTo=this.relativeTo; //<-- used on a string?
    else this.relativeTo=url;
    if(!callback) callback=this.loaded;    //<-- used on a function?
    var req = new XMLHttpRequest();
    if(req) {
               // request handling code                
            }
        };
        req.open("GET", url, true);
        req.send("");
    }   
}

In this library I've seen many uses of this operator in this manner.

Can someone explain how/if the 'not' function of a string, object or function can be determined when it isn't one half of a Boolean set like the set; true and false?

like image 221
Zeeno Avatar asked Aug 09 '11 16:08

Zeeno


People also ask

How is the NOT Boolean operator represented in JavaScript?

(NOT) The boolean NOT operator is represented with an exclamation sign ! . The syntax is pretty simple: result = !

What does operator mean in JavaScript?

In JavaScript, an operator is a special symbol used to perform operations on operands (values and variables). For example, 2 + 3; // 5. Here + is an operator that performs addition, and 2 and 3 are operands.

How do you indicate a Boolean NOT operator?

The logical operator *NOT (or ¬) is used to negate logical variables or constants. Any *NOT operators must be evaluated before the *AND or *OR operators are evaluated. Any values that follow *NOT operators must be evaluated before the logical relationship between the operands is evaluated.


2 Answers

Any falsy value will satisfy the if(!insert_variable_here) condition, including:

  • false
  • null
  • undefined
  • The empty string ''
  • The number 0
  • NaN

If callback return evaluates any of those values, the condition will be satisfied.

Even though null != false, the following will give you an alert:

x = null;
if(!x) {
    alert('"!null" does evaluate to true');
}

Regardless of whether or not null != false makes sense to you or anyone else, the point is that in JavaScript null is a falsy value, and thus a value that would satisfy the condition in my first bit of code listed above. This, it seems, is the question you have asked--not, rather, if null should or should not == false.

like image 116
GarlicFries Avatar answered Nov 01 '22 03:11

GarlicFries


In JavaScript, the unary negation operator (!) will convert its operand into a boolean based on the (somewhat confusing) rules of the language (e.g., ECMA-262 5th Edition). This article on JavaScript syntax shows some examples of how the type conversion happens.

Basically, it's an easy way to test for non-"truthiness"; seemingly false values (e.g. false, null, 0, NaN, the empty string, etc.) will be converted to false before being logically negated, and vice versa. You can test for "truthiness" explicitly by using the Boolean constructor:

Boolean(null); // => false
Boolean(NaN); // => false
Boolean(""); // => false
Boolean(0); // => false
Boolean(1); // = >true
Boolean("123"); // => true
Boolean(new Object()); // => true
Boolean(function(){}); // => true
like image 36
maerics Avatar answered Nov 01 '22 03:11

maerics