JavaScript Unary negation(-) Operator
Unary negation(-) operation is a single operand operator (which means it worked with only a single operand preceding or succeeding to it), which is used to convert its operand to a negative number.
Syntax:
-Operand
Example 1:
Javascript
<script> const a = 20; const b = -a; console.log(b); console.log(typeof b); const x = '20'; const y = -x; console.log(y); console.log(typeof y);</script> |
Output:
-20 number -20 number
Example2 : Working with numbers.
Javascript
<script> const x = 30; const y = -x; console.log(y); console.log(typeof y);</script> |
Output:
-30 number
Example 3: Working with non-numbers.
Javascript
<script> const x = "30"; const y = -x; console.log(y); console.log(typeof y);<script> |
Output:
-30 number
Supported Browser:
- Chrome
- Edge
- Firefox
- Safari
- IE
- Opera


