1

I am a beginner in javascript , I was trying to play with variables until i reached to this issue, when i have a variable called name , and i commented its definition step , it still has the same value , although i commented it . and this happens only with keyword "name" , so what is it ?

<script>
   // var name="mina"; i commented it now , so if you want to test , enable it once , and then comment it , it will still give you the result although i commented it and deleted the cache
  //  var name;
alert(name);
</script>
mina
  • 141
  • 1
  • 10
  • you might have global name variable some where else. – mehari Dec 02 '16 at 08:17
  • It's a global which is the name of the current window, normally `""`. –  Dec 02 '16 at 08:17
  • Javascript code isn't run in isolation - there's always a context. In your case, that context is the `window` object, so your `name` actually refers to `window.name` - the name (not title) of the window. Learn about scoping and global context in Javascript, or you're going to get quite a few surprises :) – Luaan Dec 02 '16 at 08:25

2 Answers2

4

name is not a reserved word in Javascript. As the sample code is not executing within any specific scope (within a function ect.) the name variable is referencing window.name.

console.log(name);
// output: "" (window.name)

(function(){
 console.log(name);
})()
// output: undefined

See https://developer.mozilla.org/en/docs/Web/API/Window/name for more details.

Anth12
  • 1,869
  • 2
  • 20
  • 39
  • than you senior,i tried the below code , and give me the same result alert(name); alert(window.name);, this means that window has a property called name and when we setting name we actually setting window.name , but how can i avoid this or avoid overwrite properties of windows or something else – mina Dec 02 '16 at 08:29
  • or when my variables will be added to window or something static variables ? – mina Dec 02 '16 at 08:32
  • In this case because your script is executing in the global 'window' scope you cannot create a variable with the same name. `var name = 'test'` would override `window.name`, I recommend executing your script within a function (second example) so that the local scope does not override window properties. – Anth12 Dec 02 '16 at 08:34
  • @Anth12 In chrome, name is behaving strangely like global object from localhost. i.e var name ='...' is modifying original window.name and therefore 'name; is available outside the fn scope. However when I run it separately in console and Plunkr its showing undefined/or Original window Object. In IE its behaving normal(i.e undefined/empty in localhost) – Amitesh Nov 13 '17 at 09:18
0

The variable is not in a function, so it's a global variable. Global variables are added to the window and it stays there (until you refresh the page if I recall correctly).

You don't reset the variable so it will keep having the same value.

Joshua Bakker
  • 2,288
  • 3
  • 30
  • 63
  • i refreshed it many times and have the same although it was commented , and still till now has the definition value and also i deleted the cache and the same – mina Dec 02 '16 at 08:20
  • @mina I think it will stay there until you close the browser or for a certain time. One thing; you shouldn't use global variables if you want to prevent this from happening. Even better; not mixing up HTML with Javascript. Instead, use separate files and functions. – Joshua Bakker Dec 02 '16 at 08:22
  • @JoshuaBakker the global scope is in no way persistent. Any new properties added will be gone immediately on referesh, they don't "linger on". However, if something is present at page load, it's most likely that it'd be present next page load, too, because something consistently adds it. That's in general, anyway - `name` is a normal property of `window`, so you'd expect it to always be there. – VLAZ Dec 02 '16 at 08:29
  • @vlaz:so how can i avoid keep using or overwriting the reserved window properties or any reserved object properties – mina Dec 02 '16 at 08:37
  • @mina look at how [closures work](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work?rq=1) first to get the basics and then look at modules: [link1](http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html) [link2](https://addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript). It's all the same concept, really but it's somewhat different plays on it. In essence, closures (modules _are_ closures) avoid you leaking stuff to the global scope. – VLAZ Dec 02 '16 at 08:42