almazmusic
almazmusic

Reputation: 154

What is the difference between assign and return?

I think I'm too stupid to understand the difference between:

1.

var a, b;
a = 1;
b = a;
console.log(b); // 1

2.

var x, y;
x = 2;
y = function (z) {
  return z;
}
console.log(y(x)); // 2

I just can't understand. Is this matter the data is a Number or an Array/Object?

Thanks.

Upvotes: 2

Views: 896

Answers (2)

Assignment operators assign values to JavaScript variables. return stops function execution and returns a value from it, you can "assign" that value using the = sign or discard it

https://www.w3schools.com/js/js_assignment.asp

https://www.w3schools.com/jsref/jsref_return.asp

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1075049

Assignment operations and returning values out of functions are two separate things.

A lot of programming can be thought of as moving values from one place to another.

Assignment is taking a value from one place and storing it in another. In your code, for instance:

a = 1;

the expression on the right-hand side (1) is evaluated, and the resulting value (1) is stored in a. (Yes, 1 is an expression; in this case, it's a literal expression that gives us the value 1.) Similarly:

b = a;

the expression on the right-hand side (a) is evaluated, and the resulting value (1) is stored in b.

Returning something does two things (in JavaScript and most — but not all — other languages)

  1. It evaluates the expression after the return keyword, and then sets the resulting value as the return value of the function.
  2. It ends the function, returning back to the point where the function was called.

With functions, what we do most of the time (all of the time in JavaScript) is pass values into the function, let the function do things with those values, and then have the function give us back a value (the result of calling the function).

In your:

var x, y;
x = 2;
y = function (z) {
  return z;
}
console.log(y(x)); // 2

we already know how x = 2 works (see a = 1 above). Then we do:

y = function (z) {
  return z;
}

and, as always, the right-hand side expression (function(z) { return z; }) is evaluated, which creates a function object, and then the resulting value of that (the reference* to the function) is stored in y.

So far so good.

Now:

console.log(y(2)); // 2

There, we're doing this:

  • Evaluating the innermost expression (2), and getting its resulting value (2)
  • Calling y and passing that value into it
  • Inside y, it receives the value in its z parameter, almost like you'd reached into the function and put the value into a variable called z inside the function
  • Then we do return z, which evaluates the expression after the return (z), and then terminates the function, returning the resulting value (2 in this case)
  • The result of calling y is therefore the value 2
  • The whole process happens again with console.log, which uses its parameter to output the value 2 to the console

* "reference" - In JavaScript and most (but not all) other programming languages, we don't actually copy objects around; instead, we pass arounda value called an object reference, which is basically something that tells the language runtime where the object is in memory (like a street address says where your house is). Objects aren't values, but object references are values. Since functions in JavaScript are objects, y = function() { } creates a function and then stores the reference to it in y.

Upvotes: 4

Related Questions