/    Sign up×
Community /Pin to ProfileBookmark

Use string as parameter to variable?

I want to send as string and get variable content. Two ways are often mentioned – window[] and eval(), but none of them work for me.

Any other way to “convert a string to a variable” that works?

https://jsfiddle.net/bh4ty8n7/

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumDec 05.2022 — I was really surprised that the variable is not a member of the window object and found the explanation here:

https://stackoverflow.com/questions/60346763/why-global-let-variables-not-added-to-window-object-of-browser

When defining the variable by use of `var</C> everything works fine. Additionally I had to add the parameter <C>main</C> when calling the function and set the parameter <C>sub</C> in quotes as it's the name of the variable, a string, not it's content. For clarity I changed the parameter's name to <C>subName</C>.
<CODE>
`<i>
</i> var sub = "json";

//using window[]
function fillsubmenu(main, subName) {
console.log(window[subName])
}
fillsubmenu('x', 'sub')

//using eval()
function fillsubmenu2(main, subName) {
console.log(eval(subName))
}
fillsubmenu2('x', 'sub')<i>
</i>
``
Copy linkTweet thisAlerts:
@pryinfestedFeb 17.2023 — I think you can find more information at https://stackoverflow.com/questions/60346763/why-global-let-variables-not-added-to-window-object-of-browser/ [basket random](https://basketrandom.io)

It functions correctly, even on touch devices; however, there is no zooming feature and no constraint that the image should not exceed the borders of the container. The image is free to go beyond those boundaries.
×

Success!

Help @sibert spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...