0

I'm using MDN guide to learn JS and I use scratchpad to try some examples.

If I write

console.log('The value of b is ' + b);
var b;

Why does console.log say "The value of b is -1" ?! It should be undefined

Ricardo Rocha
  • 14,612
  • 20
  • 74
  • 130
  • On what page were you opening the scratchpad? Likely it did define a global variable with the value `-1`. Notice that when you take away the `var b;` declaration, the output won't change (or does it?). – Bergi Aug 09 '17 at 08:35
  • You are right it's the same even when I remove var b; I open it using Firefox – AbedEl-Rahman Sholi Aug 09 '17 at 08:44
  • Yes, Firefox, but don't you have any tabs open? Also try `console.log(window)` and show us what you get – Bergi Aug 09 '17 at 09:02

1 Answers1

0

Probably you defined the var b before with the value -1.

If you try the above code, you will see that, if the variable is already instantiated and if you declare again the variable without set any value to them, the variable it will not be instantiated.

var b = -1;
console.log(b)
var b;
console.log(b)
Ricardo Rocha
  • 14,612
  • 20
  • 74
  • 130