Rubaninferno
Rubaninferno

Reputation: 65

How to call Array name with a string in JS

const EleList = [1,2,3]
name = 'Ele'

const render = function(type){
    window[type + 'List'].forEach(function(value){
    console.log("LOL")


render('Ele')

What am i supposed to replace the window[name + 'List'] with to call an array using strings.

Upvotes: 0

Views: 1257

Answers (1)

XCS
XCS

Reputation: 28177

const or let variables are not added to the global window object.

Replacing const with var should solve your problem.

var EleList = [1,2,3]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

Global constants do not become properties of the window object, unlike var variables.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

Just like const the let does not create properties of the window object when declared globally (in the top-most scope).

Upvotes: 1

Related Questions