6

I did this by accident...

var numbers = [1, 2, 3, 4];
numbers.push[5];

Why wasn't there an error message?

push needs parentheses, not square brackets. It was just a simple typo. I wasn't paying close enough attention to what I was doing... but why wasn't there an error message?

As far as I can tell, the numbers array wasn't modified in any way. It just did... nothing.

2
  • 1
    because that simply evaluates to undefined - any property (functions are just properties in javascript anyway) can have properties ... Commented Jan 14, 2018 at 23:20
  • 3
    Because it is just accessing the property named 5 on the property named push. There isn't a syntax error there, just a logic one Commented Jan 14, 2018 at 23:21

1 Answer 1

9

numbers.push is simply a function but you are attempting to find the property located at key 5 from it, which will evaluate to undefined.

function test() {
  console.log("test");
}


// test[5] evaluates to `undefined` and does nothing
console.log(test[5]);

// We can even manually set this without messing up the function
test[5] = "foo";

// outputs "foo"
console.log(test[5]);

// outputs our expected value "test"
test();

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

Comments

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.