When i put console.log
for any variable in browser console which are undeclared it will return Uncaught ReferenceError: variable is not defined.But when i put console.log(name)
in the browser console it returns empty and undefined. See the image below. any ideas why this happens..
I tested it in Chrome and Firefox developer tools.
Note : I use clear() to clear the console
name
is a global variable which is in the window
object. So when you log, it goes and finds the global one, which's value is empty string (""
) in your case.
console.log(name);
console.log(window.name);
This is What is happening when you type the statement console.log(name)
:
name
from within the global execution context(Logging in to the console in your case).window
object has a property that's called name, because in the browser the global scope is represented by the window
object.window.name
or just name
should return name is not defined.window
object has a set of pre-defined/native properties and name is one of them.window.name
has by default the value "" (empty string), so it logs an empty string to your console.Now this is what's happening when you type console.log(name100)
:
Same as before(name100 instead of name).
Same as before(name100 instead of name).
You've not declared name100 neither is it a native property of the window object, so it simply returns name100 is not defined
.
If you wanna check properties that is the shipped with the window object you can check this link:
W3schools reference for the window object
MDN reference for the window object
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With