JavaScript 中的一元否定運算子 (-) 是什麼?


一元否定運算子首先將運算元轉換為數字,然後對其取反。它對單個運算元進行操作。它返回運算元的否定值。布林運算元轉換為 0 或 1,然後進行否定。類似地,基數不是十進位制的數字首先轉換為十進位制,然後計算否定值。

語法

以下語法將向您展示如何使用一元否定運算子對數字的值取反:

-x

這裡一元運算子 (-) 對 x 取反。

讓我們透過程式碼示例在不同場景中瞭解一元否定運算子的應用。

演算法

步驟 1 - 宣告一個變數併為其賦值。

步驟 2 - 使用一元運算子 (-),如語法定義。一元運算子位於運算元之前。

步驟 3 - 列印否定後的結果。

示例

您可以嘗試執行以下程式碼,以瞭解如何在 JavaScript 中使用一元否定運算子:

<html>
   <body>
      <script>
         var a = true;
         var b = '0xFF';
         var c = false;
         var d = 100;
         var linebreak = "<br />";
         var result1, result2, result3, result4;
         result1 = -a;
         result2 = -b;
         result3 = -c;
         result4 = -d
         document.write("-true = "+ result1);
         document.write(linebreak);
         document.write("-'0xFF' = " + result2);
         document.write(linebreak);
         document.write("-false = " +result3);
         document.write(linebreak);
         document.write("-100 = " +result4);
      </script>
   </body>
</html>

示例

以下程式碼示例將說明在使用者輸入的字串值的情況下使用一元否定運算子:

<html>
   <body>
      <h2> Working with Unary Negation Operator (-) in JavaScript </h2>
      <p> Enter any number: </p>
      <input type = "text" id = "inp" /> <br>
      <p> Press 'ENTER' or Click the below button to see the results. </p>
      <button onclick = "display()"> Click to See Results </button>
      <p id = "result"> </p>
      <script>
         var result = document.getElementById("result");
         function check() {
            var inp1 = document.getElementById("inp");
            var inpVal = inp1.value;
            var newVal = -inpVal;
            result.innerHTML += " The negative of the entered value is: " + newVal;
         }
         function display() {
            check();
         }
         window.addEventListener('keypress', e => {
            if (e.key === 'Enter') {
               check();
            }
         });
      </script>
   </body>
</html>

在上面的示例中,我們以字串的形式從使用者那裡獲取了一個數字作為輸入,然後使用一元否定運算子對其取反,該運算子將輸入數字的型別轉換為數字並否定其值。

示例

在下面的示例中,我們從使用者那裡獲取一個數字作為輸入。否定輸入數字。顯示否定前後輸入的型別。

<html>
   <body>
   <h2> Unary Negation Operator (-) in JavaScript </h2>
   <p> Enter any number: </p>
   <input type = "number" id = "inp1" /> <br>
   <p> Click the below button to see the results. </p>
   <button onclick = "display()"> Click to See Results </button>
   <p id = "result"> </p>
      <script>
         var result = document.getElementById("result");
         function check() {
            var inp1 = document.getElementById("inp1");
            var inpVal = inp1.value;
            var newVal = -inpVal;
            result.innerHTML += " The negative of the entered value is: " + newVal + "<br> type of the entered value is: " + (typeof inpVal) + "<br> type of the negative value is: " + (typeof newVal) + " <br> ";
         }
         function display() {
            check();
         }
      </script>
   </body>
</html>

在上面的示例中,我們透過將輸入的 type 屬性的值替換為數字,以數字的形式從使用者那裡獲取輸入。之後,我們使用一元否定運算子對使用者輸入的值取反。

在本文中,我們學習了 JavaScript 中的一元否定運算子。我們藉助兩個不同的程式碼示例詳細討論了它,其中每個示例都反映了它在不同場景中的使用。

更新於: 2023年1月6日

297 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告