Is 'name' variable works like Cookies or how does it work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 14

Is 'name' variable works like Cookies or how does it work?

I think that about 'name' variable works odd because if it below alert box or document.write ect will not show undefined. Is 'name' variable special or what? Is 'name' variable works like Cookies or how does it work? You can check my code to understand what I mean if you don't have the answer you can upvote the question to make it popular to make who knows answer. At the first time I imagined it a stupid Question then I tried many times then I posted this question to discuss with others. At the end thank you 💕 https://code.sololearn.com/W57kLiCR845f/?ref=app

21st Jun 2019, 11:12 AM
Basel.Al_hajeri?.MBH()
Basel.Al_hajeri?.MBH() - avatar
11 Answers
+ 5
BroFarOps ,Luis Febro 🇧🇷 ,and daneillonge it was solved and the real problem was in the first multiple comments so I made it as comment and show a hasOwnPeoperty to explain window.name is true and window.age is false that is because name is a property name of window........... and thanks Prometheus ¯\_(ツ)_/¯ to remember that.
22nd Jun 2019, 12:26 PM
Basel.Al_hajeri?.MBH()
Basel.Al_hajeri?.MBH() - avatar
+ 10
At top level, declaring var name makes the interpreter think it is window.name. To avoid use let or const.
21st Jun 2019, 11:43 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 6
So I am going to read more about window properties to make it sure and to find the real answer ;)
21st Jun 2019, 12:10 PM
Basel.Al_hajeri?.MBH()
Basel.Al_hajeri?.MBH() - avatar
+ 5
Oh you remember me that window.name : Gets or sets the name of the window. <solved>
21st Jun 2019, 12:01 PM
Basel.Al_hajeri?.MBH()
Basel.Al_hajeri?.MBH() - avatar
+ 5
hasOwnProperty is a normal Javascript function that takes a string argument. When you call shape1.hasOwnProperty(name) you are passing it the value of the name variable (which doesn't exist), just as it would if you wrote alert(name). You need to call hasOwnProperty with a string containing name, like this: shape1.hasOwnProperty("name"). Refer to similar question and answer: https://stackoverflow.com/questions/2600085/hasownproperty-in-javascript
21st Jun 2019, 1:13 PM
BroFar
BroFar - avatar
+ 4
However I still need more explaining to understand well 😕
21st Jun 2019, 12:12 PM
Basel.Al_hajeri?.MBH()
Basel.Al_hajeri?.MBH() - avatar
+ 3
window.name does exist. That's the reason returns "true" when you call hasOwnProperty. This same property returns "true" for properties assigned with null or undefined primitive values, too. I still do not know the reason.
21st Jun 2019, 11:20 PM
Luis Febro 🇧🇷
Luis Febro 🇧🇷 - avatar
+ 1
gi
24th Jun 2019, 2:47 PM
Саша Тороков
Саша Тороков - avatar
0
Window is just a special object that contains everything. Any variable you create is made in window. Example: var a = 5; Same as: window.a = 5; window[“a”] = 5;
21st Jun 2019, 1:40 PM
YoBoi
0
variable are being comment out if you run with variable defined var name = 'name' var age = 22 alert(window.hasOwnproperty("name")) alert(window.hasOwnproperty("age")) it will all alert true because whatever we write and run codes are inside javascript module and module is a function which has properties name, undefined etc. function(name, undefined, ...,){ } so window has own property call name but not age for more check 'js module mdn'. or check on youtube programmingwithmosh nodejs module.
22nd Jun 2019, 3:54 AM
Danielov
Danielov - avatar
- 1
Luis Febro 🇧🇷 unless the property is assigned a class string or value it is the same as the value of 0 or empty. https://code.sololearn.com/WqDEfT1gJEFh/?ref=app
22nd Jun 2019, 12:10 AM
BroFar
BroFar - avatar