JavaScript Arithmetic Unary Negation(-) Operator
The 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, if it isn’t already a negative number.
Syntax:
-Operand
Example 1: This example shows the use of JavaScript Unary negation(-) Operator.
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
Example 2: This example shows the use of JavaScript Unary negation(-) Operator while working with numbers.
Javascript
<script> const x = 30; const y = -x; console.log(y); console.log( typeof y); </script> |
Output:
-30 number
Example 3: This example shows the use of JavaScript Unary negation(-) Operator working with non-numbers.
Javascript
<script> const x = "30" ; const y = -x; console.log(y); console.log( typeof y); </script> |
Output:
-30 number
We have a complete list of Javascript Operators, to check those please go through the Javascript Operators Complete Reference article.
Supported Browser:
- Chrome
- Edge
- Firefox
- Safari
- IE
- Opera
Please Login to comment...