Skip to content
Related Articles

Related Articles

Improve Article
JavaScript Unary plus(+) Operator
  • Last Updated : 02 Mar, 2021

Unary plus(+) 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 number.

Syntax:

+Operand

Below examples illustrate the Unary plus(+) Operator in JavaScript:

Example 1:

Javascript




<script>
  const x = "10";
  let y;
  y = +x;
  console.log(y); 
  console.log(typeof y);
</script>

Output:



10
number

Example 2: The following example demonstrates a unary method with numbers.

Javascript




<script>
  const a = 100;
  const b = -100;
  const c = 20;
    
  console.log(+a);
  console.log(+b);
  console.log(+c);
</script>

Output:

 100
-100
 20

Example 3: The following example demonstrates a unary method with non-numbers.

Javascript




<script>
  const a = true;
  const b = false;
  const c = null;
  const d = function(x)
        
          return x
        };
  
  console.log(+a)  
  console.log(+b) 
  console.log(+c)  
  console.log(+d)
</script>

Output:

1
0
0
NaN

Supported Browsers:

  • Chrome
  • Edge
  • Firefox
  • Safari
  • IE
  • Opera



My Personal Notes arrow_drop_up
Recommended Articles
Page :