1

let array2 = ['Banana', ['Apples', ['Oranges'], 'Blueberries']];
document.write(array2[0][0]);

I wanted to print Apples in this array. When I tried array2[0] it prints Banana which is correct, but when I make it array2[0][0] it prints B, when I make it like array2[0][1] it prints a. Seems like the string Banana became an array.

1
  • Banana is not an array, but a string, and you can access individual characters in strings with the same syntax that you use to access elements in an array - numeric brackets notation. Commented May 16, 2018 at 3:07

1 Answer 1

2

Apples is in the second positioned array. So index should be 1:

let array2 = ['Banana', ['Apples', ['Oranges'], 'Blueberries']];
document.write(array2[1][0]);

Seems like the string Banana became an array.

Please visit: String.prototype.indexOf():

Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character of a string called stringName is stringName.length - 1.

Sign up to request clarification or add additional context in comments.

3 Comments

wow!That was fast! Thank you so much! I'm new in Javascript. May I know how come the apples become 1, isn't it part of Banana array? I would be so much grateful if you could explain it to me a bit, but if you can't, it's okay. Thank you very much!
@izee, array's are 0 based....so first item at index 0, second item at 1...and so on...
oh..I just thought the code was like this array2= ['Banana', ['Apples', ['Oranges'], 'Blueberries'] ]; it looks like a nested array to me..that's why I never thought the apple is one...😁

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.