Skip to content

Unary & Double Bang !! Casting

Two short operators are widely used to convert a value from one type to another. !!value gives the value’s truthiness as a real boolean, and +value converts it to a number. Both do the same job as calling Boolean() or Number(), so the choice between them is a matter of style.

The operators are called unary because each takes one operand, in contrast to binary operators such as a + b, which take two, and the ternary conditional operator a ? b : c, which takes three.

The logical NOT operator ! converts its operand to a boolean and then flips it. Applying it twice flips the value back, which leaves a boolean with the same truthiness as the original value.

const unread = 3;
console.log(!unread);
console.log(!!unread);
console.log(!!0, !!'', !!'0', !![], !!null);

This prints:

false
true
false false true true false

The string '0' and the empty array are truthy, because the only falsy values are false, 0, -0, 0n, '', null, undefined and NaN. See Truthy and Falsey Values for the full list.

An if statement already tests truthiness, so if (!!items.length) does nothing that if (items.length) does not. !! is useful where a value is stored or returned, such as a flag in an object that will be sent as JSON, because the reader then sees true or false rather than the original value.

const user = { name: 'Sam', email: '' };
const profile = {
name: user.name,
hasEmail: !!user.email,
};
console.log(profile);
console.log(Boolean(user.email) === !!user.email);

This prints:

{ name: 'Sam', hasEmail: false }
true

Boolean itself also works as a callback, which makes it a neat way to remove falsy values from an array.

const words = ['apple', '', 'pear', null, 'fig'];
console.log(words.filter(Boolean));

This prints:

[ 'apple', 'pear', 'fig' ]

The unary plus operator converts its operand to a number by the same rules as Number(). Text from a form field or a URL is always a string, so it needs converting before arithmetic, because the binary + joins strings rather than adding them.

const qty = '14';
console.log(qty + 1);
console.log(+qty + 1);
console.log(+'', +' 7 ', +'7kg', +true, +null, +undefined);
console.log(-qty);

This prints:

141
15
0 7 NaN 1 0 NaN
-14

The conversion ignores spaces at either end, turns an empty string into 0 and gives NaN for any other text that is not wholly a number. To read the leading number from text such as '7kg', use parseInt or parseFloat. The unary minus converts in the same way and then changes the sign.

Unary plus differs from Number() in one case, which is a BigInt. + throws a TypeError on a BigInt, while Number() converts it.

const big = 10n;
console.log(Number(big));
try {
console.log(+big);
} catch (err) {
console.log(err.message);
}

This prints:

10
Cannot convert a BigInt value to a number

Where readability matters more than brevity, Boolean(value) and Number(value) say what they do in words, and many teams prefer them for that reason.

Steve Griffith covers this topic in his video JavaScript Double Bang!! Cast to Boolean.