Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is Unary Plus Operator in JavaScript?
The unary plus operator (+) is a JavaScript operator that converts its operand to a number. Unlike binary plus for addition, the unary plus works on a single value and is primarily used for type conversion from non-numeric values to numbers.
Syntax
+operand
Where operand can be any JavaScript value that you want to convert to a number.
Using Unary Plus with Numeric Strings
The unary plus operator converts string representations of numbers into actual numbers:
<html>
<body>
<script>
const x = "10";
let y = +x;
const p = "0";
let q = +p;
const i = "-10";
let j = +i;
document.write("Original: " + x + " (type: " + typeof x + ")");
document.write("<br>");
document.write("Converted: " + y + " (type: " + typeof y + ")");
document.write("<br><br>");
document.write("Zero string: " + q);
document.write("<br>");
document.write("Negative string: " + j);
</script>
</body>
</html>
Original: 10 (type: string) Converted: 10 (type: number) Zero string: 0 Negative string: -10
Using Unary Plus with Non-Numeric Strings
When applied to strings that cannot be converted to numbers, the unary plus operator returns NaN (Not a Number):
<html>
<body>
<script>
const x = "hello";
let y = +x;
const p = "123abc";
let q = +p;
document.write("String 'hello': " + y);
document.write("<br>");
document.write("Mixed '123abc': " + q);
</script>
</body>
</html>
String 'hello': NaN Mixed '123abc': NaN
Using Unary Plus with Boolean Values
Boolean values are converted to numbers: true becomes 1 and false becomes 0:
<html>
<body>
<script>
const x = true;
let y = +x;
const p = false;
let q = +p;
document.write("true converts to: " + y);
document.write("<br>");
document.write("false converts to: " + q);
</script>
</body>
</html>
true converts to: 1 false converts to: 0
Using Unary Plus with Objects and Functions
Objects and functions typically convert to NaN, unless they have custom toString() or valueOf() methods:
<html>
<body>
<script>
// Regular object
let obj = { name: 'John', age: 25 };
let objResult = +obj;
// Function
const func = function(x) { return x; };
let funcResult = +func;
document.write("Object conversion: " + objResult);
document.write("<br>");
document.write("Function conversion: " + funcResult);
</script>
</body>
</html>
Object conversion: NaN Function conversion: NaN
Objects with Custom valueOf() and toString()
Objects can define custom conversion behavior using valueOf() or toString() methods. The valueOf() method takes precedence:
<html>
<body>
<script>
// Object with toString only
let obj1 = {
name: 'Alice',
toString: function() {
return '15';
}
};
// Object with both toString and valueOf
let obj2 = {
name: 'Bob',
toString: function() {
return '10';
},
valueOf: function() {
return '25';
}
};
document.write("Object with toString(): " + (+obj1));
document.write("<br>");
document.write("Object with valueOf() priority: " + (+obj2));
</script>
</body>
</html>
Object with toString(): 15 Object with valueOf() priority: 25
Common Use Cases
| Input Type | Example | Result |
|---|---|---|
| Numeric String | +"42" |
42 |
| Boolean | +true |
1 |
| Non-numeric String | +"hello" |
NaN |
| null | +null |
0 |
| undefined | +undefined |
NaN |
Conclusion
The unary plus operator provides a quick way to convert values to numbers in JavaScript. It's particularly useful for converting string representations of numbers to actual numeric values, making it a handy tool for type coercion in JavaScript applications.
