!+x operator JavaScript

Viewed 51

my first question here! Apologies if a duplicate - couldn't find an answer.

I'm a beginner in JavaScript, currently doing some exercises and I've just ran across a line I can't grasp the meaning of. Here it is:

!+x && x !== '0'

What does !+x perform here? Line's an excerpt from a script that validates IP adresses.

Thank you in advance!

1 Answers

It checks if the x variable, when converted into a number, is falsey (0 or NaN), and isn't the literal string '0'.

Eg '0.00' will fulfill the conditions, as will 'foo', but '0' won't.

The + is unary +, converting the expression that follows into a number. Then the ! negates the boolean value of that number.

Related