top | item 28658299

(no title)

idolaspecus | 4 years ago

This is in my opinion actually a decent interview question. If you know JS in detail, if you know how the "+" operator works, it's a super easy question. If you don't know JS in detail, if you don't know how the "+" operator works, it's pretty tough. If you want an engineer who knows JS in detail, asking this question can be valuable.

discuss

order

colordrops|4 years ago

"+" isn't some singular operator. They are different operators for different types using the same character. You'd have to memorize a table of all JS types and what "+" does for each, which is silly considering that the only place it should really be used in modern JS is arithmetic and the occasional string concatenation.

dorgo|4 years ago

afaik these two places ( arithmetic and string concatenation ) are the only ones where + operation is defined. JS picks one of these two operations and casts the operands accordingly.

Edit: Wait, there is also the unary + which takes only one argument, but it also could be considered arithmetic.

Aeolun|4 years ago

That may be relevant if you are working on a javascript engine, not if you build webapplications.

afiori|4 years ago

it would be more relevant to ask about the valueOf method that can mess up all the various conversions.