[Solved] Unable to declare variable named “location” in JavaScript


Global variables in the browser are automatically properties of the window object. Assigning to window.location is how you perform a redirect in Javascript. E.g.

window.location = 'http://www.google.com';

will redirect the page to Google.

An empty URL means to use the URL of the current page, so you’re telling it to redirect to itself, which just keeps reloading the page.

Rename your variable so it doesn’t conflict with any of the window properties that have special meaning. Or make it a local variable inside a function; you can use an IIFE for this.

(function() {
    var location = '';
})();

solved Unable to declare variable named “location” in JavaScript