Reputation: 850
I was just randomly practicing JS code today and I put this line of code and ran the code.
var name = 45;
console.log(typeof name);
It told me the type of variable name is a string. It's very strange but type of Name is String yet when I typed this:
var age = 45;
console.log(typeof age);
But here type of variable age is Number. Why am I observing this kind of inconsistency? is it some convention or something like this?
Upvotes: 3
Views: 168
Reputation: 4106
If you were running this in a browser, then I think it's because the default execution context is the window object. Basically, every global value you declare becomes a property of the window object, and vice versa: every property of the window object is available as a global variable (e.g console
). Window objects have a name
property by default, and redeclaring it as a variable doesn't affect that. Anyway, that's the closest I can get to an explanation.
Upvotes: 5