3

Can anyone walk me through what JavaScript is doing here.

var myArray = ["Hello", "world"]
console.log(myArray[length])

We get the output as Hello.

3
  • I get a Syntax Error. minimal reproducible example please! Commented May 20, 2018 at 11:25
  • What output were you expecting instead?
    – Barmar
    Commented May 20, 2018 at 11:38
  • I was expecting undefined. MyArray[2] 2 being the length of the array
    – Rush
    Commented May 20, 2018 at 11:39

2 Answers 2

7

length is defined in window, which is 0 if you don't have frames.

So, myArray[0] is "Hello", while myArray[1] is "world"

0
1

Are you going to achieve this?

const myArray = ["Hello", "world"]
console.log(myArray[myArray.length - 1])
1
  • Not trying to achieve anything just wanted to understand from where it was picking the value of length as 0
    – Rush
    Commented May 20, 2018 at 11:47

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.