0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

否定( NOT )の基本 ! (+単項プラス)

Posted at

「0を1に、1を0に」──めちゃくちゃ単純な話のはずなのに、1回つまづいた…。


🧩 問題概要

入力が 0 または 1 のとき、論理的に反転して出力する。

  • 入力例:0 → 出力:1
  • 入力例:1 → 出力:0


❌ NGコード

console.log(!A);
  • !0true
  • !1false

出力が boolean になってしまう!数値が欲しいのに!


✅ OKコード

console.log(+!A);

これは !A で反転 → + で数値化(単項プラス)。

  • +!0+true1
  • +!1+false0


💡 技術ポイントまとめ

  • !A は「boolean反転」だけど、型が boolean
  • +!Abooleannumber に変換(true1, false0
  • Number(!A) でもいいけど、+!A の方が短くてスマート



僕の失敗談(´;ω;`)と解決話!🚀

0
0
1

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?