1

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 ?

Walfrat
  • 5,363
  • 1
  • 16
  • 35
  • 1
    It's implicit numeric conversion. `-variable` is an implicit `variable *= -1` – VLAZ Sep 27 '16 at 07:32
  • strange i did search for '+myVar' on google but i didn't get that. Sorry for the dup – Walfrat Sep 27 '16 at 07:33
  • 1
    @Walfrat Searchs on operators in programming are always hard until you just look at the list of operators in a good documentation (the MDN is the best one for JS). – Denys Séguret Sep 27 '16 at 07:34
  • In fairness, google isn't really good on searching programm-y things like `+word` as it will search for `word`. – VLAZ Sep 27 '16 at 07:34
  • @DenysSéguret will remeber this, thanks for the tip. – Walfrat Sep 27 '16 at 07:36

2 Answers2

3

+something is an expression using the plus unary operator.

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

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
1

+ is unary operator, which tries to get numeric value from variable.

There is a thread about it.

Community
  • 1
  • 1
Jakub Jankowski
  • 731
  • 2
  • 9
  • 20