0

I just ran into this by chance when demonstrating variable scoping in this repl. Take this code for example:

var myFunc = function() {
    var name = 'Matt';
    var functionScopedVariable = "I'm in the function"
    console.log(name); // => 'Matt'
    console.log(functionScopedVariable); // => "I'm in the function"
}

myFunc();
console.log(name) // => ''
console.log(typeof name) // => string
console.log(functionScopedVariable) // => ReferenceError: functionScopedVariable is not defined

I thought this might be some weird namespacing with repl, however go to any website, open up your console, and type name and it'll output "" instead of ReferenceError: name is not defined. What is going on here? It's not breaking anything, just super curious. I've googled around a bit but I can't seem to find anything on this topic.

2
  • 5
    because the global object, window has a name property - this isn't a "javascript" default as such (e.g. nodejs does not have a global name property), this is to do with HTML/DOM - for instance, you also have things like Image as a global, nothing to do with the javascript language though Commented Mar 8, 2018 at 0:01
  • And that's specific to browsers, not Javascript. Got it. Commented Mar 8, 2018 at 0:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.