0

Using the Javascript hoisting concept, I tried the following. I am not sure why the name variable has been picked up but not the age.

console.log(name); //Lydia
  console.log(age); //error
  age  = 28;
  name = 'Lydia';
  console.log(name);
  console.log(age);
ramya sri
  • 21
  • 3
  • 1
    The output will definitely not be `Lydia` for the first one. It's going to be the content of [`window.name`](https://developer.mozilla.org/en-US/docs/Web/API/Window/name) - very likely an empty string. – VLAZ Jul 15 '21 at 14:06
  • If you're thinking about hoisting then I think you should put var before your variable name. Then you're declaring the variable and may assign or read from it anywhere. – michal.materowski Jul 15 '21 at 14:09

1 Answers1

1

The problem is that name is a global property available via the window object. So what your getting is a false positive.

  
  console.log(name);
silencedogood
  • 3,209
  • 1
  • 11
  • 36