LoginSignup
0
2

More than 5 years have passed since last update.

!!を2つ使う論理演算子

Posted at

人の書いたjsを改修することになって読んでいたら論理演算子で「!」が連続して2つ書かれていた

こんな感じ


hoge = 'ほげ!!';

if(!! hoge){
 console.log(1);
}else{
 console.log(2);
}

結果は「1」

調べた結果、どんな処理をしているのか

!! は ! の処理を2回行っている
notの後にnotだから元に戻る

だから、!! は

if(hoge){}

これと変わらない状態になるけど ! でboolean型になることに意味があるのかな?
コードを読んでも意図がわからなかった、、、

phpの場合

話を変えてphpだとしたらisset関数を使って、

$hoge = 'ほげ!!!';
if(isset($hoge)){
 echo '中身あります';
}

となるところを !! 使えば簡略化できるなーと思いました。

!! は正直明確な用途がわかんない、、、

0
2
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2