Global and local variables with the same name

(Krisztian Toth) #1

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

"Mozilla splash page" assessment
(Ajay Nara) #2

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

(Krisztian Toth) #3

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

(Chris Mills) #4

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.

(Krisztian Toth) #5

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