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

  • Home
  • SEARCH
  • 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 8556059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:15:39+00:00 2026-06-11T15:15:39+00:00

Possible Duplicate: Calling a JavaScript function named in a variable I want to use

  • 0

Possible Duplicate:
Calling a JavaScript function named in a variable

I want to use the value of a variable as the call to initiate a function, but I’m not sure if it is possible?

say I have:

function myFunction(){
    // Do something
}

var functionName = "myFunction";
functionName(); // This should run the function named myFunction

Is this something that can be done?

  • 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-11T15:15:40+00:00Added an answer on June 11, 2026 at 3:15 pm

    You could do a few things:

    //if the function is in the global scope:
    
    window["myFunction"](); 
    //or
    var functionName = "myFunction";
    window[functionName]();
    

    Or maintain your own map:

    var functions = {
        myFunction: function() {
            ...
        },
        ...
        myOtherFunction: function() {
            ...
        }
    };
    

    Then either of the following will work:

    functions.myFunction(); //will work
    functions["myFunction"](); //will also work;
    
    var functionName = "myFunction";
    functions[functionName](); //also works
    

    Of course, you can use eval:

    eval(functionName + "()");
    

    But eval is dangerous and I don’t recommend it.

    Based on your fiddle

    Your right and left functions have been defined in the local scope of an anonymous function. This means that they are not part of the global window object. You should use the second method I described above (constructing your own map):

    var directions = {
        right: function() {
            ...
        },
        left: function() {
            ...
        }
        ...
    };
    
    var direction = "right";
    directions[direction]();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: calling ASP function from javascript okay running this code : <script type=text/javascript>
Possible Duplicate: Calling PHP Function within Javascript I have a small JavaScript code in
Possible Duplicate: Function triggering early calling a functions value I need some help. I
Possible Duplicate: Calling base method using JavaScript prototype I want to inheritance object that
Possible Duplicate: Javascript closure inside loops - simple practical example Calling setTimeout function within
Possible Duplicate: Calling Python from JavaScript I have a test.py and test.js . I
Possible Duplicate: Calling NSLog from C++: “Format string is not a string literal (potentially
Possible Duplicate: How to Use setTimeout in a for…loop calling setTimeout with a for
Possible Duplicate: Calling a method named “string” at runtime in Java and C I
Possible Duplicate: C++ virtual function from constructor Calling virtual functions inside constructors This question

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.