JavaScript tricky Question Part-1

By

JavaScript Tricky Question
In this post i will discuss some tricky question in JavaScript.Also i explain this question and answer if you have tricky question the let me know i will post this question in my website.
I hope my virtual friends are aware about the JavaScript Array. My first question from Array.



Code Sample [JavaScript]


Question 1.
        var arrCode = ['a', 'b', 'c'];
        alert('First elemnet value = ' + arrCode[0] + ';Second elemnet value = ' + arrCode[1] + ';Third elemnet value = ' + arrCode[2]);

Explanation:
       All of you able to give answer of above question. Perfect you are right lets take another. 

Question 2.
         var arrCode1 = [1 + 3, 7 - 1, 1 + 6];
        alert('First elemnet value = ' + arrCode1[0] + ';Second elemnet value = ' + arrCode1[1] + ';Third elemnet value = ' + arrCode1[2]);

Explanation:
        Its little bit confusing but i tell you answer its 4 for arrCode1[0],6 for arrCode1[1] & 7 for arrCode1[2].Its because it sum the number and give the output.Now i modify some thing in above array and ask another question.

Question 3.
       var arrCode2 = ['1 + 3', '7 - 1', '1 + 6'];
        alert('First elemnet value = ' + arrCode2[0] + ';Second elemnet value = ' + arrCode2[1] + ';Third elemnet value = ' + arrCode2[2]);
         
Explanation:
        Its again bit confusing but i tell you answer its '1+3' for arrCode2[0],'7-1' for arrCode2[1] & '1+6' for arrCode2[2].Its because its treated as string and give the output as like as exist.Now i modify some thing in above array and ask another question.

Question 4.
        var arrCode3 = ['1' + '3', '7' - '1', '1' + '6'];
        alert('First elemnet value = ' + arrCode3[0] + ';Second elemnet value = ' + arrCode3[1] + ';Third elemnet value = ' + arrCode3[2]);
        
Explanation:
         Now tell me just above question output.For answer just click the below output in result pane.
    

OutPut


4 comments:

  1. nice tricky question specially 4th one

    ReplyDelete
  2. from 4th question why is string '7' - '1' is 6 ?

    ReplyDelete
    Replies
    1. Its because - is not used in string concatenation meanwhile + is used for both int as well as string.
      When ever you use - then its treat as - as you know the functionality.

      Delete