hackwithharsha
hackwithharsha

Reputation: 910

Variable undefined in Java script

function getForm()  
{   
  var arry =[ [0,1] ,[1,2],[2,3],[3,4]];         
  var a;     
  var x = 1 ;        
  console.log(x);     
  for (i = 0; i < arry.length; ++i)      
  {         
    if (arry[i][0] == x)          
    {             
      a = arry[i][1];          
    }         
    console.log(a);  
  } 
}

I am totally new to JavaScript, I was running this programming in Firebug. I have got the output as undefined I was expecting the output x=1,a=2(I mean first console.log and second one). I was defined all the variables inside the function only.

Upvotes: 2

Views: 597

Answers (5)

Jaykumar Patel
Jaykumar Patel

Reputation: 27604

Your mistake is console print outside the if condition, so first time condition going false (return undefined) and second time condition satisfied (then print correctly),

you should move console into if condition,

<script>
    var arry =[ [0,1] ,[1,2],[2,3],[3,4]];         
    var a;     
    var x = 1 ;        
    console.log(x);     
    for (i = 0; i < arry.length; ++i){         
         if (arry[i][0] == x){        
            a = arry[i][1];          
            console.log(a);  
         }         
    } 
</script>

Console Result:

 1
 2

View on jsFiddle

Upvotes: 2

gettingLearned
gettingLearned

Reputation: 126

You declared the function but never invoked. Invoking the function will give you the desired results, since when I tried to execute the statements without function wrap, i.e

    var arry =[ [0,1] ,[1,2],[2,3],[3,4]];
    var a;     
    var x = 1 ;        
    console.log('Value of x', x);     
    for (i = 0; i < arry.length; ++i) {         
       if (arry[i][0] == x) {             
          a = arry[i][1];          
       }         
       console.log('Value of a in ', i , 'iteration' ,a);  
    } ;

this was the output:

Value of x 1

Value of a in 0 iteration 2864415817892.274

Value of a in 1 iteration 2

Value of a in 2 iteration 2

Value of a in 3 iteration 2

Upvotes: 0

super_user
super_user

Reputation: 143

console.log(x) is working fine

in first interation , the if condition is false , so 'a' will not get initialized and hence in console you will get undefined , next iteration onwards you will get the desired console output,

if you are running on firebug extra 'undefined' will be displaed because calling a function which does not return anything will result 'undefined' on the console

Upvotes: 0

David
David

Reputation: 218798

It outputs undefined because the first time you call console.log(a) the variable a is undefined.

You declare a without a value:

var a;

Then in the first iteration of the loop you check for this:

if (arry[i][0] == x)

When i is 0, arry[i][0] is also 0. And you defined x as 1. 0 does not equal 1, so this condition is false. Which means the code inside the conditional block, which sets a value to a, doesn't execute. But then the code after it does.

At that time a is undefined, so console.log(a) logs undefined.

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816242

You are logging a in each iteration, but a is not set until the if statement is true, which only happens in the second iteration.

Lets go through it step by step:

// 1. iteration
i = 0
arry[i][0] == x => false // 0 == 1
console.log(a) // undefined

// 2. iteration
i = 1
arry[i][0] == x => true // 1 == 1
a = arry[i][1] // a = 2
console.log(a) // 2

// 3. iteration and beyond
// the condition is always false, so `a` keeps its value
i = 2
arry[i][0] == x => false // 2 == 1
console.log(a) // 2

Upvotes: 0

Related Questions