开发者

Meaning of the += operator when using a function?

开发者 https://www.devze.com 2023-02-11 17:22 出处:网络
If I see something like this: myVariable += myF开发者_StackOverflowunction(); How does that work? Like, for example, is the myFunction() function supposed to return a value that is added to myVaria

If I see something like this:

myVariable += myF开发者_StackOverflowunction();

How does that work? Like, for example, is the myFunction() function supposed to return a value that is added to myVariable?


That's correct. myFunction() is evaluated first, (e.g., it'll run and return its value), then its return value will be added to myVariable.


Depends on your the result of your function and the value of your existing value that your are adding.

  1. If the preceding myVariable is a string and the result is a number the values will be concatenated as a string.

  2. If the preceding myVariable is a number and the result is a number the values will be handled as a sum of numbers.

  3. If the preceding myVariable is a number or string but the result is a string the myVariable will be treated as a string, and the values will be concatenated.


I assume it's the equivalent of

myVariable = myVariable + myFunction();
0

精彩评论

暂无评论...
验证码 换一张
取 消