What is Unary Negation Operator (-) in JavaScript?


The Unary Negation Operator first converts the operand into a number, and after that it negates. It operates on a single operand. It returns the negation of the operand. A boolean operand is converted to 0 or 1, and then negation is done. Similarly, a number with a base other than decimal is first converted to base 10, then the negation is computed.

Syntax

The following syntax will show you how you can use the unary negation operator to negate the value of a number −

-x

Here unary operator (-) negates x.

Let us understand the application of the unary negation operator with the help of code example in different scenarios.

Algorithm

Step 1 − Declare a variable and assign a value to it.

Step 2 − Use the unary operator (-) as the syntax defines. The unary operator precedes the operand.

Step 3 − Print the result after negation.

Example

You can try to run the following code to learn how to work with Unary Negation Operator in 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>

Example

The below code example will illustrate the use of the unary negation operator in case of the string values entered by the user −

<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>

In the above example, we have taken a number as input from the user in the form of the string and then negate it using the unary negation operator which turns the type of the input number to number and negate its value.

Example

In the example below, we take a number as input from the user. Negate the input number. Display the type of input before and after negation.

<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>

In the above example, we have taken the input from the user in the form of a number by replacing the value of the type attribute of the input to the number. After that, we used the unary negation operator to negate the value entered by the user.

In this article, we have learned about the unary negation operator in JavaScript. We have discussed it in detail with the help of the two different code examples, where each one of them reflects the use of it in different scenarios.

Updated on: 06-Jan-2023

245 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements