In my Javascript code += increments the number but ++ doesn’t. Could somebody explain why?
Doesn’t increment
words[splitted[i]] = ( typeof words[splitted[i]] != 'undefined' )
? words[splitted[i]]++
: 1
Does increment
words[splitted[i]] = ( typeof words[splitted[i]] != 'undefined' )
? words[splitted[i]] += 1
: 1
Sample code is here
Your code, in a simpler form, will potentially result in code of this form when
words[splitted[i]]is defined:x++returns the value of x, and then increments it, and then sets the value returned tox. This contrasts withwhere x is incremented first and then evaluated.
To see how this works, look at the following code:
In any case, I think what you are really wanting to do is something along these lines:
This is more lines, but might be more readable: