Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialsyalih
16,468 PointsJavaScript .push() TypeError: undefined is not a function
var name = ['math','john']; name.push('alice');
when I try to run this code I face this issue "TypeError: undefined is not a function "
1 Answer
Marcus Parsons
15,719 PointsHey again Syalih,
When you are initializing var name
you are actually pushing the values from the array into window.name
which changes the name of the window. And when you try to push new values into name
it is not allowing you to do that because name
is still a reference to window.name
which is a string, not an array.
To get around this, use another variable such as "names". Try to stay away from words that are too generic such as just "id" or "name" as they will likely be keywords in JavaScript. This works perfectly:
var names = ['math','john'];
names.push('alice');
syalih
16,468 Pointssyalih
16,468 PointsThank You :)
Marcus Parsons
15,719 PointsMarcus Parsons
15,719 PointsYou're very welcome, Syalih! Happy Coding! :)