1

var sum = 2;

function addFive() {
  var sum = sum + 5;
  console.log(sum); //why not 7
}
addFive();

Why is it returning NaN instead of 7?

FZs
  • 11,931
  • 11
  • 28
  • 41
Subham Bisoyi
  • 51
  • 1
  • 5
  • 4
    Because you are declared a local variable `sum` inside the function. Remove `var` before `var sum = sum+5;` – Maheer Ali Aug 05 '19 at 10:56
  • The newly declared variable `sum` inside the function is shadowing the outer `sum`. You only need `var` when you introduce a new variable. – Chris G Aug 05 '19 at 10:58
  • try `sum = sum+5;` without the `var` so that you are not declaring a new variable by the same name. – Thomas Aug 05 '19 at 11:02

3 Answers3

5

To make things clearer, your code is essentially being read like so:

var sum = 2;

function addFive() {
  var sum; // implicitly equal to undefined
  sum = sum + 5; // sum = undefined + 5 = NaN
  console.log(sum); // NaN
}
addFive();

As you can see, you're redeclaring sum and it is set to undefined. So, when you try and add 2 to undefined you're going to get NaN (Not a Number).

Instead, you can reference your outer sum variable by not redefining it within your function and instead just reassign its value:

var sum = 2;

function addFive() {
  sum = sum + 5; // sum = 2 + 5 = 7
  console.log(sum); // 7
}
addFive();
Nick Parsons
  • 31,322
  • 6
  • 25
  • 44
2

You're declaring the variable sum again in the function. This has all to do with scoping in JavaScript.

Take a look at the following article to learn more.

In this case, the local variable (the one that's declared inside the function) gets priority by the JavaScript parser. This behavior's called 'shadowing'. It starts at the innermost scope being executed at the time, and continues until the first match is found, no matter whether there are other variables with the same name in the outer levels or not.

StefanN
  • 756
  • 5
  • 16
  • var sum = 2; function addFive (){ console.log(sum); } addFive (); This returns 2.. I don't understand.. why can't I declare the variable again as a local scope ? – Subham Bisoyi Aug 05 '19 at 11:00
  • @SubhamBisoyi `why can't I declare the variable again as a local scope ?`, you can and you have, and that's why your answer is 2. Otherwise what you want is global scope. – Keith Aug 05 '19 at 11:04
  • @SubhamBisoyi Or, you can even declare a local variable, but you'll have to use a different name for it, to make it work – FZs Aug 05 '19 at 11:11
1

Because you are declared a local variable sum inside the function. You have to use this.

var sum = 2;

function addFive(){
  var sum = this.sum + 5;
  console.log(sum);
}

addFive();

In this context, this will refer to the global object (in a browser, window), so the above code is equal to:

var sum = 2;

function addFive(){
  var sum = window.sum + 5;
  console.log(sum);
}

addFive();

You can read more about this in this SO post or on MDN

FZs
  • 11,931
  • 11
  • 28
  • 41
Alex Palaz
  • 117
  • 1
  • 6
    Using `this` here is confusing and in strict mode will fail. – Keith Aug 05 '19 at 11:06
  • now I get it.Thanks a lot. How can I mark this question as answered ? – Subham Bisoyi Aug 05 '19 at 11:07
  • 4
    Let's hope you never want to start using modern js modules, or `use strict;` as you might have a fair bit of code to repair.. :) – Keith Aug 05 '19 at 11:17
  • Would be frustrating to use `window.sum` in nodejs – Mark Aug 05 '19 at 11:27
  • @Mark You might want to look into `globalThis` for that very reason. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis – Keith Aug 05 '19 at 11:28