1
Creating a function to return a given value, from received parameters, it modifies the variable it receives.
For example:
var a = ["oi", "tchau"];
function duplicar(c) {
var b = c;
b[0] = b[0] + b[0];
b[1] = b[1] + b[1];
b = b.join(" ")
return b
}
console.log('Variável antes da função: "' + a + '"')
console.log('Retorno da função: "' + duplicar(a) + '"')
console.log('Variável após função: "' + a + '" (não quero que isso aconteça)')
That’s not the function I created in my code, but the same thing is happening. I am quite beginner in Javascript, I do not know why this occurs nor how to prevent.
I want the function to return a value, modifying the variable to only within it, without modifying the variable a in the source scope.
I’ve tried using Let, const, objects...
The problem is probably why you did not declare the variable B within the function. Thus, the parameter she is receiving is A, and sub-variable b by a. It was clear?
– lvr7