Global and local variables with the same name

Thank you. What about if I have global variable with the same name?

Its always local variable first then global scope. You can check with below snippet.

1 Like

Thank you very, much, although, I don’t see the snippet.

The parameter names you specify inside a function definition are recognised only inside the function. It is better to not call them the same as global variables to avoid confusion, but it could still work.

For example, this would be fine:

function add(number1, number2) {
  return number1 + number2;
}

var number1 = 'A nice sunny day';

var myResult = add(2, 3);

In this case, the number1 inside the function has a scope of that function only - it isn’t available in the global scope.

The number1 outside is in the global scope. So there is no collision.

1 Like

Thank you. I also just found the Functions article, and it explains scope there.