0

When I use "name" as a variable name and use var keyword, the "name" variable isn't initialized as array.

var name = ['banana', 'kiwi'];
Object.getPrototypeOf(name) == String.prototype; // true

But when I use const keyword it works normally.

const name = ['banana', 'kiwi'];
Object.getPrototypeOf(name) == Array.prototype; // true

Or when I use a different variable name, it works normally, too.

var name2 = ['banana', 'kiwi'];
Object.getPrototypeOf(name2) == Array.prototype; // true

Why does this happen?

User9728
  • 59
  • 5
  • (1) As you've noticed, don't use `var` - it's 2022, use at least ES2015 syntax when possible (2) Run scripts inside IIFEs to control their scope better – CertainPerformance May 04 '22 at 02:12
  • the problem is ... global `name` in a browser is *special* ... as you found out, almost any other variable name other than `name` would work just like you expect - read [window.name](https://developer.mozilla.org/en-US/docs/Web/API/Window/name) docs ... it may tell you whats going on (it may not) ... also, `const`/`let` in global scope are different to `var` – Bravo May 04 '22 at 02:19

0 Answers0