0

Below code, I need to access variable a inside call function

$(document).ready(function(){
var a=10;
call("a");
function call(x) {
//alert 10 using x
}
});

Please dont change the code, just write the correct alert statement

arjit nair
  • 51
  • 2
  • 5

1 Answers1

0

Try to do like this,

$(document).ready(function(){
  var a=10;
  call(a);
  function call(x) {
   alert(x);
  }
});

In your code you are passing the variable as a string. so it won't do the things as are expecting. You may use eval() for this purpose, that is reading a value from the string formatted variable name. But it is not a recommended one since it is modifying the scope at runtime.

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130