3 Replies - 722 Views - Last Post: 13 June 2018 - 12:25 AM

#1 kayut   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 20-January 17

Function and accessing the global variables

Posted 07 June 2018 - 04:39 AM

Hey,

If I run this:

let number = 10;
function increment(number) {
	number++;
	console.log(number);
}

increment();


I expect to get 11.
Because functions have access to the global scope, hence the function increment should have access to the global variable number with the value 10.
Then it should increment number by 1 and log 11.

But the result is NaN!!

It seems that number inside the function cannot access the global variable number and therefor number inside the function is undefined. And undefined +1 results in NaN.

But why is that the function increment cannot access the global variable number??

Thanks

This post has been edited by kayut: 07 June 2018 - 04:39 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Function and accessing the global variables

#2 mkkekkonen   User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 71
  • Joined: 10-May 18

Re: Function and accessing the global variables

Posted 07 June 2018 - 04:41 AM

Because you define number as a function parameter and it overrides the global variable.

Either remove number from the parameter list or pass the global variable as a parameter to the function.

This post has been edited by mkkekkonen: 07 June 2018 - 04:43 AM

Was This Post Helpful? 2
  • +
  • -

#3 Pogaca   User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 128
  • Joined: 24-August 13

Re: Function and accessing the global variables

Posted 12 June 2018 - 09:36 PM

var number = 10;

function increment(n) {
	n++;
	console.log(number);
return n;
}

var n = increment(number);
console.log(n);


It is simple. You used function with no arguments in it. You created function declaration with number as a parameter but when you called function you omitted that one. So, remember that n++; works only after block of code is done. First console log will print 10. After you call a function get value from it, as I did with return. Then console log the value and it will be 11. If you wanted right away to see first value 11, you should used inside function declaration ++n;
Was This Post Helpful? 1
  • +
  • -

#4 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 895
  • View blog
  • Posts: 3,607
  • Joined: 13-June 14

Re: Function and accessing the global variables

Posted 13 June 2018 - 12:25 AM

Yeah, sadly Javascript allows you to omit passing values for parameters in a function rather than producing an error. You should avoid global variables - allowing state to be modified from anywhere and everywhere is not a good idea because it can make it hard to track down where the changes have been made and that can lead to difficult to diagnose problems.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1