/    Sign up×
Community /Pin to ProfileBookmark

reading value of javascript variable with concatenated call

I am setting some javascript variable names to name1, name2, name3, etc.

I would like to concatenate “name” with a number in a separate javascript function, and get the value of the javascript variable.

An example would be :

var name1 = “one”;
var name2 = “two”;
var name3 = “andSoOn”;

function myfunction(theNumber)
{
var getthatvariable = “name” + theNumber;

alert(“this is the name for that number ” + getthatVariable);
}

I have not been able to get this to work.

Can you ??

(thank you for input as always.)

}

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@vinays84Sep 17.2008 — [CODE]
var name1 = "one";
var name2 = "two";
var name3 = "andSoOn";

function alertValue(varNum) {
var varName = "name" + varNum;
var varValue = eval( varName );

alert("The value of "+ varName +" is "+ varValue);
}

alertValue(1);
[/CODE]
Copy linkTweet thisAlerts:
@mparker1113authorSep 17.2008 — eval () was exactly what i was looking for !

thanks

[CODE]
var name1 = "one";
var name2 = "two";
var name3 = "andSoOn";

function alertValue(varNum) {
var varName = "name" + varNum;
var varValue = eval( varName );

alert("The value of "+ varName +" is "+ varValue);
}

alertValue(1);
[/CODE]
[/QUOTE]
×

Success!

Help @mparker1113 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 5.5,
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,
)...