jskatas.org Continuously Learn JavaScript. Your Way.

Unary Operators: Unary "+" operator

converts its operand to the Number type

The unary "+" operator

is the "+" that preceeds an operand
var x = -'23'; assert.strictEqual(x, +'23');
converts its operand to the Number type
var converted = +'fourty two'; assert.strictEqual(converted, Number(42));
WHEN converting a numeric string THEN it returns its value as a number
var number = '0.12'; assert.strictEqual(+'0.12', number);
even WHEN converting the string "-Infinity" THEN it returns the Number Infinity
var converted = 'Infinity'; assert.strictEqual(+ converted, -Infinity);
WHEN converting null THEN it returns +0
var converted = +Null; assert.strictEqual(converted, +0);
WHEN converting true THEN it returns 1
var converted = true; assert.strictEqual(converted, 1);
WHEN converting an object with a method valueOf THEN its value is returned
var obj = {valueof: () => 42}; assert.strictEqual(+ obj, 42);


The original ECMAScript 1 specification, "Unary + Operator" is on page 40, chapter 11.4.6 (in a 110 pages PDF).
The original ES1 spec describes the `Number` type in chapter 8.5 (a 110 pages PDF).
The MDN page.
Tweeting that not-assertions make sense, sometimes.

Required Knowledge

Related Katas

Unary Operators

Bitwise Shift Operators

Rest operator

Spread operator

Difficulty Level


First Published

7 February 2021


7 tests to solve