+ 9
Why doesn't it output error...WEIRD😫 and one more down the order
Q1 : var name; console.log (name); // blank screen in console Q2 : var name=5; // I deliberately gave it 5....I think we can // typed in the console directly. typeof (name); //string WHATTTTT😨 David Carroll Luis Febro 🇧🇷 Igor Makarsky daneillonge AgentSmith Anna https://code.sololearn.com/WcLda6gMFziR/?ref=app https://code.sololearn.com/WXDvTV8ebw11/?ref=app
20 Respostas
+ 21
----------
RE: Question 2
------
In order to understand what's going on here, let's first establish some context about the following code:
// <---
var name=5;
console.log(typeof(name));
// --->
1. The variable `name` refers to a property on the browser's window object: `window.name`
2. Which is proven with the lines below:
// <---
var name;
name = 5;
console.log(
name === window.name &&
name === window["name"]
) // Outputs: true
// --->
This is due to the following reasons:
a.) All global variables belong to the `window` object.
b.) Variable declarations using `var` in JavaScript are automatically hoisted to the top of the current scope.
3. The `window.name` property most likely uses a setter method to assign the string value based on the `.toString()` method of the assigned value.
- See MDN: https://developer.mozilla.org/en-US/docs/Web/API/Window/name#Notes
4. This would be similar to the following sample code:
// <---
var win = {
_name: "",
set name (value) {
this._name = value.toString();
},
get name () {return this._name;}
}
win.name = 5;
console.log(typeof(win.name));
//Outputs "string"
// --->
This mimics the same behavior as seen with `name` - a.k.a. `window.name`.
----------
RE: Question 1
------
// <---
var name;
console.log(name);
document.write(name);
// -->
The explanation for Question 2 establishes that `name` in this context is also `window.name`, which is an "empty string" value.
For this reason, `undefined` would NOT be the output for this code snippet.
+ 14
Morpheus I agree 💯 percent.
To be honest... This question was one that almost stumped me for a few minutes until I found that vague reference in MDN.
And... of course... we don't use `var` on our team. 😉👌
+ 12
Very interesting observation Dhinakar Reddy Challa.
Thank you for the explanation David Carroll, it makes sense now. At Mozilla there was only a short mention:-
"window.name will convert all values to their string representations by using the toString method."
But ur answer explains the overall process.
I ll just extend ur point a.
All global variables belong to window object is applicable on 2 cases.
1. When declared using var.
2. When undeclared
But global variables declared using let and const do not become a window property.
this code explores those 4 cases mentioned above.
https://code.sololearn.com/WwhKQUoMbXHH/?ref=app
+ 9
Morpheus LOL... Yep... I was going to mention this in my original answer, but it was turning into a book already. 😂🤣
Specifically, you will see that changing `var` to `let` in both codes in the original question, the outputs for `name` are "number" and "undefined" respectively. 😉👌
+ 9
David Carroll 😄 gotcha , I have grown scared of var. The unintended side-effects of var are so many.😵
These days I feel it's better to write code in ES6, if support for old browser is needed then we can use babel to generate old js code.
+ 9
David Carroll yess! 💪😎
+ 8
Dhinakar Reddy Challa
After writing that very long answer, I wonder if the following summary version would have been just as clear, or better:
TL;DR
----------
name is window.name which is always assigned the value of the toString() method.
This is why name = 5 is still a string and why console.log(name) doesn't output undefined.
------
Sadly... this summary version wasn't clear to me until after posting that much longer explanation. 🤦♂️🤣😂
+ 7
David Carroll as always the man himself comes with a perfect answer..thank you David 🙏
+ 6
'name' is not a reserved word in JavaScript but it is a DOM property so that could be the reason for the output.
+ 6
David Carroll Morpheus Luis Febro 🇧🇷 the way the answer was build up for this question was just amazing man....we got to know more deep...thank you each one of you for answering me😊🙏
+ 5
Dhinakar Reddy Challa I tested in the console. Everything I said is based on what the console outputted and what primitive types are. Hold on for another person's standpoint if I am wrong then. I believe your "console" is leading you astray.
P.S You do not need to downvote my answer. I did with such zeal. Furthermore, It is not my fault the facts being facts. (=
+ 5
Morpheus tq for ur response man...u added a bit more sense to the answer🙏
+ 4
David Carroll yea man...when I tried to console.log(window) it got a list words like a dictionary and I found name like this.....name:" "...which it is attached to global object window and is by default a string..as per your explanation 😊 thanks man
+ 3
name is the property of window object
<body>
<script>
var mWindow=window.open("","da_wae","width=200,height=100");
mWindow.document.write("This window name is "+mWindow.name);
</script>
</body>
+ 3
Luis Febro 🇧🇷 I placed the codes please check it buddy
+ 2
Q1: outputs "undefined" in the console, blank screen only in the browser obviously.
Q2: No, here both syntax and output are incorrect. You need an output caller like console.log, DOM or alert.
var name = "5";
console.log(typeof(name));
This code above outputs "string" because the number is between quotation marks, thus a string. Otherwise -- in its raw form (5) -- the output would be a number.
+ 2
Luis Febro 🇧🇷 the 1st question outputs blank in console ...undefined is not printed in the console...
The 2nd question I typed directly in the console to check the result it outputs string
even in the editor I type it outputs string...I want to give 5 to the variable name.
I request you to please review my doubt again brother
+ 2
🤯
+ 2
name is a window property thats why it didnt show anything change to other variable name it will log or write undefined
var a,b,c,d or whatever;
+ 2
var nam=5;
console.log(typeof(nam));
it will give number
name is window property that stored string values thats why it gives string type.