0

My Sample Code

 function testfunction(){
     var index = 0;
     $(string).find("data").each(function () {
         if(index == 0){
             var testVar    = 'test'; // Declaring a variable
         }
         index++;
     });
     console.log(typeof testVar); // Shows undefined
}

output : undefined

Here, I am not able to access the variable testVar outside the loop.

Anyone please let me know why this is happening, As I am creating a variable inside the function and accessing the same variable inside the same function.

Kathir
  • 1,212
  • 15
  • 25

0 Answers0