How do I call a function inside of another function?

JavascriptFunction

Javascript Problem Overview


I just want to know how to call a javascript function inside another function. If I have the code below, how do I call the second function inside the first?

function function_one()
{
alert("The function called 'function_one' has been called.")
//Here I would like to call function_two.
}

function function_two()
{
alert("The function called 'function_two' has been called.")
}

Javascript Solutions


Solution 1 - Javascript

function function_one() {
    function_two(); // considering the next alert, I figured you wanted to call function_two first
    alert("The function called 'function_one' has been called.");
}

function function_two() {
    alert("The function called 'function_two' has been called.");
}

function_one();

A little bit more context: this works in JavaScript because of a language feature called "variable hoisting" - basically, think of it like variable/function declarations are put at the top of the scope (more info).

Solution 2 - Javascript

function function_one() {
  function_two(); 
}

function function_two() {
//enter code here
}

Solution 3 - Javascript

function function_one() { alert("The function called 'function_one' has been called.") //Here u would like to call function_two. function_two(); }

function function_two()
{
    alert("The function called 'function_two' has been called.")
}

Solution 4 - Javascript

function function_first() {
    function_last(); 
    alert("The function called 'function_first' has been called.");
}

function function_last() {
    alert("The function called 'function_last' has been called.");
}

function_first();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWeb_DesignerView Question on Stackoverflow
Solution 1 - JavascriptChristianView Answer on Stackoverflow
Solution 2 - JavascriptLuthozView Answer on Stackoverflow
Solution 3 - JavascriptRajendra TripathyView Answer on Stackoverflow
Solution 4 - JavascriptSuresh MadhaiyanView Answer on Stackoverflow