1
first();
function first(){
    second();
    third();
}
function second(){
    var b='second';
}
function third(){
    console.log(b);
}

Getting error while trying to access variable b in third(), can you please help console.log(b); ^

ReferenceError: b is not defined

Paul
  • 26,170
  • 12
  • 85
  • 119
  • 2
    You might want to read [What is the scope of variables in Javascript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Paul Sep 11 '17 at 03:47

1 Answers1

2

if you want to access b than you have to define it as global

    first();
 var b;
  function first(){
     second();
    third();
 }
function second(){
     b='second';
}
function third(){
    console.log(b);
}

console.log(b);

look my bin global b

kumbhani bhavesh
  • 2,189
  • 1
  • 15
  • 26