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

Var names and Keywords

I was working on the variable challenge in the javascript basics course and for a var name I used "location" and when I tried to run the code I got an error page and the console said not allowed to load local resource. I didn't find location as a keyword but when I changed it to "place" it worked. Any ideas as to why this happened?

2 Answers

location might not be a keyword, and it's not a reserved word either (some words are reserved for future use) but it does have a special meaning. It is a variable which browsers themselves tend to register as a global variable. location is an object that is used to get info from the browser about the current page location and methods that allow you to change it.

So for example this code:

console.log(location.href)

will print out the URL of the current page you are on. And this code:

location = "https://google.com"

Will actually cause a page redirect to the URL specified.

You can do a couple other things with location as well but that should give you the gist of what it is used for.

Steven Parker
Steven Parker
243,228 Points

It might not be a language keyword...

But location is the property of the browser's global window object that holds information about the current location of the document, along with methods that can be used to manipulate it.

For example, if you assign a valid URL string to the location variable it will cause your browser to display that page, just as if you had selected it from a link or typed it into the address bar. And you can refresh the current page using this code:

location.reload(true);

There are a number of other useful things that can be done with location. See the MDN page for thorough coverage of the details.