+12

Why the output is 4?

var arr = [ ]; arr [0] = arr.length; arr [1] = arr.length + 1; var x = arr.length + arr[1]; document.write(x) ________________________ /* If arr[0] length is 1 arr[1] length is 2 arr[1] = 2 + 1=3 arr.length = 2 +arr[1] = 3 Answer is 5 Why output is 4 😥 */

5/19/2019 7:53:50 AM

Tahir Usman

17 Answers

New Answer

+28

As the array begins from 0 1 2 3 and so on so are.length+1=1+1=2 And go on increase

+14

AWI thanks, I understand that but if arr = [0]; arr.length = 1 arr = [1] length should be 2 because we have two numbers in the same array,,,, Why it is considering each individually 😑 that was my point..!!

+11

Emmanuel Joshua but arr=[0,1] arr.length=2+1

+11

AWI it means the arr.length is being considered for the individual array not for the whole array.

+9

This is the flow: var arr =[] ; arr[0] =arr.length; //this stores 0 as the first value of the array with a length of 1. arr[1] = arr.length + 1; //arr.length is 1 added to 1 to give a value of 2 By now arr has this arr =[0, 2] with the length of 2 var x = arr.length + arr[1]; /* arr.length is 2, while arr[1] is 2 So, x = 2+2 which is 4. */

+9

Emmanuel Joshua but arr[0] length should be 1 not 0,

+9

Thanks David Carroll ,you all are so nice to me. Love you all😍. It takes some time to be clear, everyone answered well enough to be understood but I had no patience that time. This is a fault of mine to learn everythings in a moment. If I read it now, everyone answered clearly😍

+8

Tahir Usman I'm assuming you might be learning to program for the first time. If so, then, it's easy to lose your patience on concepts in the beginning. It does help to walk away and return when you've cleared your mind. The challenge is getting your mind to think like an imperative programmer, which is much different from a declarative programmer. Rest assure... there will be a point where all this effort to keep concepts straight in your mind will be effortless. I swear, I can program with less effort than it takes me to write this response in English - my native language. 🤣 You'll get there. Just keep asking for help from the community. 😉👌

+6

Initially the length of arr is zero since it is empty, and zero in an integer value. arr[0] = arr.length; //assigning the first index of array the value of 0. arr[1] = arr.length+1; //arr.length = 1 (from arr[0]), therefore arr.length+1 = 1+1 = 2. var x = arr.length+arr[1]; //arr.length = 2 (ie arr[0] and arr[1]), and the value stored at index 2 is 2 (from arr[1]=2), therefore arr.length + arr[1] = 2+2 = 4.

+5

Tahir Usman it is considered for the whole array. There are different ways of initializing arrays and the method you use is one of them. When you give an index a value, it automatically appends itself to the array giving the array a new length.

+5

Assignment operation is a two steps operation : step 1: evaluate the right side of =. step 2: assign the value obtained in step 1 to the variable at left side. In the statement "arr[0]=arr.length;", the value of arr. length is fetched first and it returns 0 as array is empty at that point. Then 0 is stored at arr[0] and now length is 1. Only after completion of entire statement arr. length becomes 1. Same with the next statement, first the value of arr. length is accessed which is 1 and then 1+1=2, 2 is stored in arr[1].

+5

Awesome dialog in this thread. I'm only sorry I missed it 27 days ago. Mad props to Emmanuel Joshua, AWI, nidhi for the patience and ability to give such clear and well written explanations based on the feedback focusing on the remaining points of confusion. 👏 I'd like to give even greater props to Tahir Usman for pressing on with where the confusion remained. It was clear that he was trying to wrap his head around the explanations provided. But rather than leaving it be, he did his best to articulate exactly where the disconnect was in his mind. I hope the understanding was made clear with the response from nidhi. However, if it's still not clear, please let us know. It's great for me to see dialogs like these from time to time because it's easy to forget what might not make sense for people in the process of learning these concepts. Well done to all involved. 🤓👌

+4

Tahir Usman it's how you choose to initialize your array. Yours is an empty or null array, so that later you can give each indexes a value. You can refer to this lessonhttps://www.sololearn.com/learn/JavaScript/1240/

+3

Tahir Usman Of course, the length is 1,while the first value of the array is 0 👍

+3

arr[1]=2 not 3 the arr.lenth for a[0]=1 Emmanuel Joshua i think you are right. thanks

+2

nidhi thanks.. thats what i wanted to clarify my doubt

+2

arr[0]=arr.length if arr[0] length is 1 arr[0] = 1 arr[1] = arr.length + 1 arr[1] = 1+1 = 2 if arr[1] length is 2 var x = arr.length + arr[1] var x = 2+2 = 4 So output is 4