jmls
jmls

Reputation: 2969

what is the meaning of +!! in javascript?

I came across this line of code recently, and want to understand what it means and does, as my javascript-foo is not that hot :

if ((+!!config.template) + (+!!config.templateUrl) !== 1) {}

from what I can gather, it is checking to see if either option is set (so either template, or templateUrl must be set, not both or none)

so, if config.template was set,

if config.template was not set,

if then you were to apply the same to config.templateUrl , you would end up with 1 if set, 0 if not

So the final test is to see if the sum of config.template and config.templateUrl is 1 (ie one or the other is set)

Is this valid reasoning ?

Upvotes: 1

Views: 73

Answers (1)

RoryGilchrist
RoryGilchrist

Reputation: 118

The bool value is being cast to a Number by prepending it with +.

!! in the code above is checking for existence of the property template on config. If there is no template found !! would usually return false, however by prepending +, it returns 0. Both +!! statements return numbers, which when they're added together will either be 0 or 1.

The final check will return true if both or none were set / true (!== 1)

Upvotes: 3

Related Questions