The Unary + Operator for Number Conversion
You'll learn how to use the unary + operator as a quick, explicit way to convert any value into a number.
What Is the Unary + Operator?
The unary + operator sits directly in front of a value (no second operand needed) and forces JavaScript to convert that value to a number. Think of it as a shortcut for saying "give me the numeric version of this."
You've already seen the + operator add numbers and concatenate strings. But when you place + before a single value, it becomes a conversion tool, not an arithmetic operator.
How It Works
+"42" // 42 (string becomes number)
+"3.14" // 3.14
+true // 1
+false // 0
+null // 0
+undefined // NaN
+"" // 0 (empty string)
+"hello" // NaN (non-numeric string)
The unary + follows the same coercion rules you've learned: strings with valid numbers convert cleanly, booleans become 1 or 0, and invalid inputs produce NaN.
Why Use It?
It's concise and explicit. Instead of writing Number(value) or relying on implicit coercion in arithmetic, +value clearly signals your intent to convert to a number in minimal syntax.
let userInput = "25";
let age = +userInput; // Clean, readable conversion
Key Takeaway: The unary + operator is a shorthand for explicit number conversion—place it before any value to coerce that value to a number type, making your conversion intent clear and your code compact.