The role of JS !! () of

First, let's look at the results of several expressions to run in the browser console:

 

 js novices often write such bloated code when doing input validation:

var a = 1
if(a!=null&&a!=undefined&&a!=''){
    document.write('a为非空')
}

In fact, just write

var A = . 1 
IF (!! A) { 
    document.write ( ' A non-empty ' ) 
}

It can be. Only when a non-empty when, !! a talent to true, thereby executing code in braces, achieve the effect of streamlining the code.

Guess you like

Origin www.cnblogs.com/luoyihao/p/12129393.html