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 trial

JavaScript

Javascript Array not defined error

The code below is an array I made in the atom editor, it is: const array = [1, 2, 3, 4, 5];

When I run this code in google chrome and test it with a 'array.indexOf(2)' in the console, I get an error message that states: "Uncaught ReferenceError: array is not defined."

I don't understand how my array is not defined when I defined it in the beginning with 'const'... obviously I'm missing something. Help would be much appreciated, this is driving me mad.

For anyone reading this.. I have resolved the issue. My javascript wasn't being recognized in my browser because it was not linked to an html document. I had only run my .js file in the browser. In order to have your .js file run in the browser it needs to run OFF OF your .html file. :)

2 Answers

David Mclean
David Mclean
3,646 Points

You already declared "array" as a const, meaning you can't declare it again. const means constant variable you can't change the value again.

Solution:

Refresh the page and type this:

const array = [1, 2, 3, 4, 5];
array;

I put that in my atom code, still getting the same error :/ Plus I am not trying to re-declare the variable I am only trying to have my array run so that I can use instance methods/properties for practice.

David Mclean
David Mclean
3,646 Points

There must be something else then, I don't think this is the issue.

Am I running the right code through atom? Should I be able to run that code straight from atom into chrome's console??