Walfrat Walfrat - 1 year ago 68
Javascript Question

How can '+myVar;' be a valid syntax

I just got a bug that took some of my time to spot my searching filters weren't working because of the following code :


The good syntax is to mive the ';' in the string :


The reasn why i didn't spot that is because the earlier line of code didn't triggered any javascript console error. So it seems it's a valid syntax.

So here is my question, how can this be a valid syntax ?

Answer Source

+something is an expression using the plus unary operator.

Its general purpose is to convert a value, for example a string, to a number.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download