Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript Unary negation(-) Operator

Improve Article
Save Article
  • Last Updated : 09 Jan, 2023
Improve Article
Save Article

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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!