Walfrat Walfrat - 1 month ago 6
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 :

queryObject.search='valid==true';+searchQuery;


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

queryObject.search='valid==true;'+searchQuery;


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

+something is an expression using the plus unary operator.

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

Comments