Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8739663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:58:47+00:00 2026-06-13T10:58:47+00:00

I need some help with a javascript principle. I have a variable where part

  • 0

I need some help with a javascript principle.
I have a variable where part of the variable name is “dynamic”. Let me try to explain;

var name_city = "London";
var name_code = "500";

From the webpage I got an value named “type”. This will be “city” or “code”.

(I just make it manualy here for the sake of this example)

var type = "city";

So with “city” as the value of type, I now want to alert “name_city”.
But I have no idea on how to do this with javascript.

Does anyone know?

In php I think it will be like this:

$type = "city";

$varname = 'name_'. $type;
echo ${$varname};

I appreciate any help or some clue in the right direction 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:58:48+00:00Added an answer on June 13, 2026 at 10:58 am

    In order to accomplish this, you would need to store the data within an object literal, where you can access its keys dynamically. If you write those variables in the global / window object you can access them the same way.

    // global scope
    var name_city = "London";
    
    var type = "city";
    
    console.log( window[ "name_" + type ] );
    

    This won’t work if you aren’t in the global scope (which actually, should not be the case at all). So you should store the data in a self-defined object

    var data = {
        "name_city":  "London",
        "name_code":  "500"
    };
    

    and then do the same thing, just access via

    console.log( data[ "name_" + type ] );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
I'm not really good at Javascript/jQuery yet, so I need some help. I have
I need some help about JavaScript on iPhone UIWebView ; I have HTML like
Hey. I need some help with jQuery Ajax calls. In javascript I have to
I am a total noob to jQuery and Javascript.. need some help. I have
I need some help in JavaScript, HTML, HTML DOM and DHTML. I am not
I need some help on the JavaScript. I do a ajax call on a
I`m new to JavaScript and I need some help extracting the ID from URL
Im a javascript newbie and would need some help with this simple thing... I
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.