Meaning of !! javascript

This question already has an answer here:

  • What is the !! (not not) operator in JavaScript? 31 answers

  • !! Converts any value to a boolean value

     > !!null
     false
    
     > !!true
     true
    
     > !!{}
     true
    
     > !!false
     false
    

    If a value is falsey then the result will be false . If it is truthy the result will be true .

    Even more, the third ! inverts the converted value so the above examples become:

        > !!!null
        true
    
        > !!!true
        false
    
        > !!!{}
        false
    
        > !!!false
        true
    

    It forces what is returned to be a boolean and not an integer or empty value. For example, 0 evaluates to false with == but will not with === . So to be sure that any integer 0 returned will be converted into an boolean, we use !! . This also works if null or undefined is returned.

    So whats happening is actually:

    var test = null;
    var result = !test; // returns true
        result = !return; // returns false
    

    !! is used to convert the value to the right of it to its equivalent boolean value.

    !!false === false
    !!true === true
    
    链接地址: http://www.djcxy.com/p/12658.html

    上一篇: 什么是使用的目的! 在JavaScript中?

    下一篇: 的意思 !! JavaScript的