Understanding the Unary Plus Operator (+) in JavaScript

Muthu Kumar - Jul 6 - - Dev Community

JavaScript is full of interesting and useful operators, one of which is the unary plus operator (+). You might have come across it in code snippets like +new Date(), but do you know what it really does? In this post, we'll explore the unary plus operator and its various applications.

What is the Unary Plus Operator?
The unary plus operator (+) is used to convert its operand into a number. This operator is particularly handy when you need to ensure that a value is treated as a numeric type.

Example: Converting a Date to a Timestamp
Consider the following example:
The unary plus operator (+) is used to convert its operand into a number. This operator is particularly handy when you need to ensure that a value is treated as a numeric type.

Example: Converting a Date to a Timestamp
Consider the following example:

+new Date(); // Returns the current timestamp in milliseconds
Enter fullscreen mode Exit fullscreen mode

Here, new Date() creates a new Date object representing the current date and time. The unary plus operator converts this Date object to its numeric representation, which is the number of milliseconds since January 1, 1970, 00:00:00 UTC (the Unix epoch).

Other Uses of the Unary Plus Operator
The unary plus operator isn't just limited to dates. It can be used to convert various types of values to numbers.

  • Converting Strings to Numbers
let str = "123";
let num = +str; // num is now 123 (number)
Enter fullscreen mode Exit fullscreen mode
  • Converting Boolean Values to Numbers
let boolTrue = true;
let boolFalse = false;
let numTrue = +boolTrue;   // numTrue is now 1
let numFalse = +boolFalse; // numFalse is now 0
Enter fullscreen mode Exit fullscreen mode
  • Converting null to a Number
let nullValue = null;
let numNull = +nullValue; // numNull is now 0
Enter fullscreen mode Exit fullscreen mode
  • Converting undefined to a Number
let undefinedValue = undefined;
let numUndefined = +undefinedValue; // numUndefined is NaN
Enter fullscreen mode Exit fullscreen mode
  • Converting an Object to a Number
let obj = {};
let numObj = +obj; // numObj is NaN, because {} cannot be converted to a number
Enter fullscreen mode Exit fullscreen mode
  • Converting Arrays to Numbers
let emptyArray = [];
let singleElementArray = [5];
let multipleElementArray = [1, 2, 3];

let numEmptyArray = +emptyArray; // numEmptyArray is 0
let numSingleElementArray = +singleElementArray; // numSingleElementArray is 5
let numMultipleElementArray = +multipleElementArray; // numMultipleElementArray is NaN
Enter fullscreen mode Exit fullscreen mode
  • Using in Expressions
let a = "5";
let b = 10;
let result = +a + b; // result is 15, because +a converts "5" to 5
Enter fullscreen mode Exit fullscreen mode

Conclusion
The unary plus operator (+) is a versatile tool in JavaScript that allows for quick type conversion to a number. Whether you're dealing with strings, booleans, null, undefined, or even arrays, this operator can help ensure your values are in the numeric format you need.

Next time you see +new Date() or need to convert a value to a number, you'll know exactly what the unary plus operator is doing and how to use it effectively in your code.

Happy coding!

. .