Can someone help me be better about the loop inside the function?

Hi, I’ve finished learning javascript syntax II in full-stack engineer. But until now, I’m still confused about when to use variables in functions when we want to use loops, and also, I’m still confused about where to use return when there are so many things we have to do. Please, if there is someone who understands maybe can explain, or if there is a source for learning please let me know. Thank you

Hi bellaananda194954872!
I’d like to help you, but I didn’t understand exactly what you are confused about. Could you make some examples with code? :slight_smile:

The code inside a function is no different than outside. Functions are the code wrappers we use when we do not wish to repeat the exact code block. Once enclosed by a function object wrapper, we can call the same piece of code repeatedly, into infinity.

A function object, like all other built in objects, has properties of its own. It takes input (called arguments) and processes them locally (that is, function scope), but outside of that whatever it does could just as easily be done in global scope, the one time.

Arguments passed into to a function are given names. That is what parameters are, just names for the arguments to be known by within the scope of the function. The caller does not know these names since all it provides are the arguments.

function foo (params) {    //  callee

console.log(foo(args))     //  caller

Short of accessing global variables, the argument is the only way for us to supply data to a function, and the parameter is the only way for the function to receive that data. Think in terms of connection. The function process is connected to the caller supplied data. This permits us to move data one way between scopes.

So there is no question that our function will use variables. The use of the values they represent is up to us, the writers of this block of code. Now we turn to the other way to move data between scopes, the return statement. This moves the data from function to caller scope. Outside of this mechanism, the caller has no access to data within the function. Our functions are generally designed to move data back to the caller.


When we are starting out it can be difficult to discern where this term, ‘variable’ comes from. Quite frankly, ‘LABEL’ would do just as apt. Don’t give any magical value to the term. It boils down to just a character string.

As a character string it is scalar. It is; that’s it. Given a location in memory to point to, it becomes a vector. There is a sense of direction. Something is referred to at that memory location. Stuff we work with is stored in memory. We rely upon the OS to give us some access to memory. Hence, we can assign values stored in memory to labels (also stored as character strings) in memory, in an associative way.

In many languages, a value exists, in its exact form in only one location in memory. Regardless how many processes generate a given value, if the value they generated already exists in memory, that value will be retrievable from only the location it originally existed.

We are not the least concerned with how this process is handled. We’ve got variables to point to those locations, through the language and its ability to work with the OS.

Now to consider…

let a, b, c;

a = 1;
b = 2;
c = 3;

a is not 1, and 1 is not stored in a. 1 is stored in memory and our giving it a label means we are referring to the value at that location (address). This reference is what keeps that value alive in memory, else it would vaporize.

Let’s say,

let d;

d = c - b

Now d has the same value as a. Without our doing the system will have pointed it to the same location in memory as a. In some languages we could confirm this. At the very least we can write,

d === a

If nothing else, they have the exactness of the same object.

1 Like

Let’s say we create a character string literal somewhere in our code.

literal = "some text"

Where in memory does that string live? Easy answer, wherever it lives in the source code. The first byte address becomes the reference that literal points to. There is no shifting around of data. We bind to it where it stands.

1 Like