This question already has an answer here:
- Do let statements create properties on the global object? 5 answers
- why don't const and let statements get defined on the window object [duplicate] 2 answers
I'm trying to better understand the use of this
. In experimenting with this code, I found I can access the items in arr
by using something like console.log(this.arr[4])
, but only if arr
is declared using var
. If I declare arr
using let
or const
, I get a TypeError.
First, I do understand this is verbose. Like I said, I am just experimenting to try and get a better understanding and came across this issue that piqued my curiosity.
const arr = [
1,
false,
{
name: 'John',
address: '123 Peachtree Drive'
},
function(name = 'new user') {
const greeting = 'Hello there, '
console.log(greeting + name)
},
'foo',
]
console.log(this.arr[4])
Again, if I simply declare arr
using var
instead of let
I can log it just fine.