Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript scope: passing a variable from an outer function to an inner function

I define the variable "a" in the outerFunction. I want to use it in my innerFunction. How come this doesn't work, and what is the best way to pass data between nested functions?

var outerFunction = function () {
   var a = 5;
   innerFunction();
}


var innerFunction = function () {
   alert(a);
}

outerFunction();
like image 655
Don P Avatar asked Aug 30 '25 16:08

Don P


1 Answers

You have to add parameter to inner function and pass value to it from outer function.

Live Demo

var outerFunction = function () {
   var a = 5;
   innerFunction(a);
}


var innerFunction = function (a) {
   alert(a);
}

outerFunction();
like image 97
Adil Avatar answered Sep 02 '25 07:09

Adil