What does an exclamation mark before a variable mean in JavaScript

后端 未结 4 1464
说谎
说谎 2020-11-28 08:13

I\'m trying to learn JavaScript by going through some code in an application and I keep seeing !variable in if conditions. For example:

if (!va         


        
4条回答
  •  攒了一身酷
    2020-11-28 08:56

    It is a negation operator used for truth tests on a variable.

    var myVariable = 1;
    
    if ( ! myVariable )
    {
        // myVariable evaluates as false
    }
    
    if ( myVariable )
    {
        // myVariable evaluates as true
    }
    

提交回复
热议问题