How to receive and maintain values in functions (javascript)

an update, I had a bug where it was reading from both a global variable and the passed variable, it works where either you pass or use a global variable, just not both, hence why I got undefined errors.
the following is just a solution using the global variable, but I currently simply deleted the one I was using and pass it normally.

I have finally managed to find a solution, albeit it does not solve the original question ( solved ), I solved it by simply not passing two arguments to my function and instead pass just 1 and set the other as a global variable, this I am told is not an elegant solution, there are two ways to do this you can either declare your global variable outside all your functions but this does not work for me I assume because argumentno2 comes as an argument to another function entirely from a seperate script so I used this command to make it global

anArray.filter(something)                  //passes an array to function something, the array is recieved as argument1 one by one
global.usethis_argumentno2 = argumentno2   //makes usethis_argumentno2 a global and sets its value to argumentno2

function something(argument1){             //recieves anArray as argument1
//do something with argument1
//do something with usethis_argumentno2 as its now a global

answer sourced from here

Leave a Comment