Answer the question
In order to leave comments, you need to log in
What is the difference between returning a value and assigning?
An example from a popular JS tutorial:
For example:var i = 1; var a = ++i; // (*) alert(a); // 2
On the line (*), calling ++i will increment the variable and then return its value to a. So a will get the value of i after the increment.
The postfix form i++ differs from the prefix ++i in that it returns the old value before the increment.
return
and =
? Answer the question
In order to leave comments, you need to log in
in "i = 1" you just assign the value to the variable i.
in "a = i++" you call a separate subroutine that will find the value in the variable i, add one to it, save the changed i, which will now be one more, and then return the value, and this returned value will be assigned to the variable a
Feel the difference ?
Well, yes, finish reading the textbook, the question is elementary.
var a = 1;
var b = 1;
alert(++a); // 2
alert(b++); // 1
Faced with similar questions, the creators of one of the programming languages decided to completely exclude from the language such a thing as assigning values to variables. At the same time liquidating as such the concept of a variable.
As a result, the language became cleaner and easier. And the number of questions that arise when studying it has noticeably decreased.
1) Assigning and returning a value are two different things.
An assignment is an assignment to a variable of the value returned by the expressions to the right of =.
That is, there is an expression that returns a value as a result of evaluating this very expression. For example, (a + b + c) / d. Unfortunately, an expression can not only return a value, but also change the value of the variables that participate in it. Like i++ and ++i. But this is bad coding style (my personal opinion). Return is no longer the return of a value, which was discussed above. This is an operator that specifies what value will be the result of executing the function. Just when it says return 0; then this is called returning the value of the function.
2) Crap, read the answer to paragraph 1
3) Return of value by expression can be made in anywhere. For example, it just says i++. But if this example makes sense, then if you just write a + b, then at least it’s possible, there’s no point.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question