Inline `++` in Javascript not working

It's amazing to find out that a line like this:

$('#TextBox').val(parseInt($('#TextBox').val())++ ); 

Does not work!

I did some tests, it concludes that inline ++ does not work (in Javascript in general or just in my example?).

Here's a test with three pieces of code , it seems that ++ is ok with a variable, but not inline.

So, there is no built-in ++ for us in Javascript?

+4
source share
3 answers

There is nothing special about jQuery. ++ increases the value of a variable. You are trying to increase the return value of a function call.

+8
source

Q: What does x++ mean?

A: x++ means the value of x , call this n , then set x as n + 1 , then return n .

Q: Why does this happen with non-variable?

A: Try to try something simple, say 3 and see where everything goes wrong.

  • Take the value 3 and name it n , okay, n = 3

  • Set 3 as n + 1 , so 3 = 3 + 1 , 3 = 4 it makes no sense! Therefore, if this step cannot be performed, the ++ operator cannot be used.

+4
source

++ works with variables, not directly on numbers

 var c = parseInt($('#TextBox').val()); $('#TextBox').val( ++c ); 

Reorder from

 var x = 0; var result = x++; result // 0 

For

 var x = 0; var result = ++x; result // 1 

Then it will evaluate ++ to get the value.

0
source

Source: https://habr.com/ru/post/1491327/


All Articles