Can a variable contain data from multiple functions at the same time?

Hi there! In the third chapter of the book JavaScript: Absolute Beginner’s Guide, an example is given where the variable myDistance first receives the result of calculations of the showDistance function, and then shows how the same variable myDistance receives a value from another function - getDistance. I didn’t really understand…can a variable contain data from multiple functions at the same time?

Hi @PaulAsaf2 - welcome to the forums :slight_smile:

The answer to your question is no. It will store the value of the last function that it called. If you have an example like this:

let foo = showDistance();
foo = getDistance();

The value of foo will be whatever getDistance returned. Whatever was returned by showDistance will no longer be remembered by the foo variable.

In the book, I’m essentially overwriting the original value with a new value, which is something you can do with variables declared using let or var,

Cheers,
Kirupa :desert_island:

1 Like