0

I want to know how can i do it?

e.g.

function get() {
   alert(s);
}
function declare() {
   var s = "Blah";
   get();
}

But I get that s is not defined.

I know we can do by passing it as argument and also setting it as global variable but how without both of them?

Muhammad Talha Akbar
  • 9,952
  • 6
  • 38
  • 62

1 Answers1

1

You can use a closure:

function declare() {
   var s = "Blah";
   function get() {
      alert(s);
   }
   get();
}
John Zwinck
  • 239,568
  • 38
  • 324
  • 436