
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
What is Unary Plus Operator in JavaScript?
Unary operators are the simplest operators in JavaScript which works on one value, or we can say on one operand. There are mainly six kinds of unary operators in JavaScript which are given as −
Unary Plus
Unary Minus
Increment operator (Prefix)
Increment operator (Postfix)
Decrement operator (Prefix)
Decrement operator (Postfix)
In this tutorial, we are going to learn about the unary plus (+) operator. To use the unary plus operator we just need to put a plus sign in front of the operand. This operator is basically used to convert a non-numeric value to a numeric value, and whenever we used it in some numeric value it does not put any effect on it and its value remains same.
Syntax
We can use the following syntax to use the unary plus operand −
let x = +y;
Let us look into some of the examples to understand the unary plus operator more clearly −
Example
Using Unary plus operator with the integer value
In the example below, we use the unary plus operator in front of the integer values.
<html> <body> <script> const x = "10"; let y; y = +x; const p = "0"; let q; q = +p; const i = "-10" let j; j= +i; document.write(y); document.write("</br>"); document.write(q); document.write("</br>"); document.write(j); </script> </body> </html>
As we can see here when we use unary plus operator in front of integer value it reverts us back with the same integer values we provided either it’s a positive integer or a negative integer. You can further check the result by using the operator in front of the decimal or fractional values it will give the same results as given above.
Example
Using Unary plus operator with the string value.
Here we are going to use the unary plus operator in front of the string values.
<html> <body> <script> const x = "a"; let y; y = +x; const p = "Prince"; let q; q = +p; document.write(y); document.write("</br>"); document.write(q); </script> </body> </html>
As we can see here when we use the unary plus operator in front of the string or character value it reverts us back with the “NaN” which refers to Not a Number.
Example
Using Unary plus operator with the Boolean value.
Here we are going to use the unary plus operator in front of the Boolean values.
<html> <body> <script> const x = true; let y; y = +x; const p = false; let q; q = +p; document.write(y); document.write("</br>"); document.write(q); </script> </body> </html>
As we can see here when we use the unary plus operator in front of the Boolean values i.e., either true or false. The unary plus operator converts the true value into 1 and converts the false value into 0.
Example
Using Unary plus operator with the functions and objects
Here we are going to use the unary plus operator in front of the functions and objects values.
<html> <body> <script> let x = { name: 'John', age:'10' } let y; y = +x; const p = function(x){ return x }; let q; q = +p; document.write(y); document.write("</br>"); document.write(q); </script> </body> </html>
Example
As we can see here when we use the unary plus operator in front of the functions or objects it converts both of them in NaN i.e., Not a Number.
But there is way by using which we can convert the objects into a number using the Unary plus operator and that is by using toString() or valueOf() methods inside the object as shown in below example.
<html> <body> <script> let x = { name: 'Rishit', toString: function () { return '12'; }, }; let y; y = +x; const p = { name: 'Rishit', toString: function () { return '12'; }, valueOf: function () { return '20'; }, }; let q; q = +p; document.write(y); document.write("</br>"); document.write(q); </script> </body> </html>
As we can see here we get the result as 12 when we use only toString() method and get result as 20 if we use both toString() and valueOf() method because unary plus operator give the preference to the valueOf() operator.
- Related Articles
- What is Unary Negation Operator (-) in JavaScript?
- What is overloading a unary operator in C++?
- Unary operator in C++
- Overloading unary operator in C++?
- Java Unary Operator Examples
- Overload unary minus operator in C++?
- How to solve “Unary operator expected” errors?
- What is increment (++) operator in JavaScript?
- What is decrement (--) operator in JavaScript?
- What is Conditional Operator (?:) in JavaScript?
- What is typeof Operator in JavaScript?
- What is Modulus Operator (%) in JavaScript?
- What is Multiplication Operator (*) in JavaScript?
- What is Addition Operator (+) in JavaScript?
- What is Assignment Operator (=) in JavaScript?
