Variable with same name inside function changes the value of the passed parameter

BILAL AHMAD

I have a javascript function as below :

function modifyx(xvalue) {
  val = 1;
  for (x = 0; x < 10; x++) {
    val = val + 1;
  }
  return val;
}

And the main snippet passes a variable named x to the above function as below:

for (x = 0; x < 10; x++) {
  console.log(modifyx(x));
}

The expected output should be "11" printed 10 times but instead it prints one time. The function call changes the value of x eventhough i am not modifying the passed value. The x inside the function has it's own scope. Yet it gets modified. Any help on this would be highly appreciated.

Mamun

The variable x in your code is global. When your method modifyx(xvalue) returns for the first time the value of x is already 11 which is used in the for loop for the second iteration. Thus it fails to execute the method further.

Use let x in the declaration in for (x = 0; x < 10; x++) to create a unique execution environment.

function modifyx(xvalue) {
  val = 1;
  for (x = 0; x < 10; x++) {
    val = val + 1;
  }
  return val;
}

for (let x = 0; x < 10; x++) {
  console.log(modifyx(x));
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is the function argument not overwritten on creating variable of same name inside the function?

function to print name of passed variable

Can I use the value of a variable as a parameter name for a function?

Using function parameter names that are the same as passed variables

Why kotlin allows to declare variable with the same name as parameter inside the method?

Cannot use let to define a variable with the same name of function's parameter

Local variable passed in parameters changes in the function despite not being a return value

Why can't use let to declare a variable using the same name as one function parameter, even the parameter get set default value?

Hoisting inside function having same variable name

How come I can create a variable with same name as a function parameter?

Pass a parameter into a function and have that function generate a variable name that contains the passed parameter?

determine if value passed to function is a variable

Get name of variable passed as function parameter

is rvalue passed as parameter treated as lvalue inside the function?

Variable value is not being passed inside the function

Unable to access variable inside method passed as parameter

Print name if parameter passed to function

How to receive a function parameter of any type in C++ and get the type of the passed variable inside the function?

How to call a function by its name, passed as parameter?

How to update a dictionary value when a variable changes inside a function

Hoisting inside function - inner function and variable having same name - Output?

How to use same-name global variable if passed as None to a function?

In python, is it possible to fetch variable value, where the variable name is passed as a function argument

How do I reference a function parameter inside inside a data.table with a column of the same name?

Change original variable value by changing value of passed function parameter

how we can use inheritance to access a parameter that is passed to a function with the same name but in two different classes?

It is possible to use default parameter value its same function name

Pass an @State variable as parameter and change its value inside a function

how to assign parameter passed to constructor to local private variable with the same name