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 8408357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:36:37+00:00 2026-06-09T23:36:37+00:00

I want to write something like this: function MyFunction () { …. return SomeString;

  • 0

I want to write something like this:

function MyFunction () { .... return SomeString; }
function SomeFunction () { ... return SomeOtherString; }

function SomeCode() {

  var TheFunction;

  if (SomeCondition) {

    TheFunction = SomeFunction;

  } else {

    TheFunction = SomeCode;
  }

  var MyVar = TheFunction(); // bugs here
}

Basically, I’d like to execute a function after some condition has been evaluated so that variable MyVar contains contains the result of the function that has been called.

What am I missing to make this work?

Thanks.

  • 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-09T23:36:38+00:00Added an answer on June 9, 2026 at 11:36 pm

    If you are just talking about conditionally executing a function and storing the results…

    var result;
    
    if( condition ){
       result = foo();
    }else{
       result = bar();
    }
    
    var myVar = result;
    

    If you want to determine the function to call, but not call it until another time, use:

    // define some functions
    function foo(){ return "foo"; }
    function bar(){ return "bar"; }
    
    // define a condition
    var condition = false;
    
    // this will store the function we want to call
    var functionToCall;
    
    // evaluate
    if( condition ){
       functionToCall = foo; // assign, but don't call
    }else{
       functionToCall = bar; // ditto
    }
    
    // call the function we picked, and alert the results
    alert( functionToCall() );
    

    Callbacks can be very useful in JS…they basically tell the consumer to “call this when you think it’s appropriate”.

    Here’s an example of a callback passed to the jQuery ajax() method.

    function mySuccessFunction( data ){
        // here inside the callback we can do whatever we want
        alert( data ); 
    }
    
    $.ajax( {
        url: options.actionUrl,
        type: "POST",
    
        // here, we pass a callback function to be executed in a "success" case.
        // It won't be called immediately; in fact, it might not be called at all
        // if the operation fails.
        success: mySuccessFunction
     } );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function called check_nickname() And I want to write something like this
I want to write something like: var list = new List<int>(){1,2,3}; var bigList =
I want to write a C# method that can accept any number. Something like:
I want to load this simple something into my Editor: Write:-repeat,write(hi),nl,fail. So that it
I need a function that will do something like this: $arr = array(); //
I made a function like this (no need to write the whole function here)
I want to write a java annotation which times the method call. something like
In PHP I would do something like this: function changeBgColor($boxColor,$color) { echo '<div id='.
I want to use Canvas.TextRect to write something on the canvas with 90 degree
Sometimes, I want to use an ObjectOutputStream to write something to a file or

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.