🔖

演算子小話 - !!

2021/02/07に公開

https://dev.to/deep1144/what-is-the-not-not-operator-in-javascript-32gf
を見て試した。

var banana = true;
console.log(banana);
//true
console.log(!banana);
//これはfalse
console.log(!!banana);
//これはtrue
console.log(!!!!!!!!!!banana);
//これでもtrue
console.log(!!!!!!!!!!!banana);
//これでもfalse

つまり演算子重ねても普通に使える

var banana = true;
(banana == true)?console.log("boo"):console.log("hey"); 
//boo
(banana !== true)?console.log("boo"):console.log("hey"); 
//hey
(banana != true)?console.log("boo"):console.log("hey"); 
//hey

となるんだが
こういうことをしたら

var banana = true;
(banana !!== true)?console.log("boo"):console.log("hey"); 
//Uncaught SyntaxError: Unexpected token '!'

(banana !!= true)?console.log("boo"):console.log("hey"); 
//Uncaught SyntaxError: Unexpected token '!'

(banana !! true)?console.log("boo"):console.log("hey"); 
//Uncaught SyntaxError: Unexpected token '!'

(banana !=== true)?console.log("boo"):console.log("hey"); 
//Uncaught SyntaxError: Unexpected token '='

怒られた。

Discussion