0

I would like a little guidance here. Using JS Bin for this. The whole issue here is : "name" when used as a VAR and inside an array: ---the console.log prints each letter:

var word = "Hi";
var name = ["John","Suzette","Mari-Louise","Tinus","Hendrik","Koos","Elna","Elbie"];
// Greeting
greeter(word,name);

function greeter(str,arr){
 var counter;
  for(counter = 0;
    counter < arr.length;
    counter++) {
    console.log(str + " " + arr[counter]);
    }
   }

Output

"Hi J"
"Hi o"
"Hi h"
"Hi n"
"Hi ,"
"Hi S"
"Hi u"

However, changing the VAR to userName, yields the correct result,..I cant find any reference to 'name' being a reserved word in JS, so if someone could clarify this for me, it will be smashing.

var word = "Hi";
var userName = ["John","Suzette","Mari-Louise","Tinus","Hendrik","Koos","Elna","Elbie"];
// Greeting
greeter(word,userName);

function greeter(str,arr){
 var counter;
  for(counter = 0;
   counter < arr.length;
   counter++) {
   console.log(str + " " + arr[counter]);
    }
   }

Result**

"Hi John"
"Hi Suzette"
"Hi Mari-Louise"
"Hi Tinus"
"Hi Hendrik"
"Hi Koos"
"Hi Elna"
"Hi Elbie"
Hendrik
  • 137
  • 1
  • 2
  • 6
  • This question has been asked and answered many times here on SO. Please search harder. By the way, what do you mean by "using `name` inside an array"? –  Sep 02 '16 at 03:20
  • @torazaburo: I think this really is an unsearchable issue when you don't know what happens. Of course, `typeof name` could give you a hint, but still… – Bergi Sep 02 '16 at 03:25
  • @Bergi You're right. I looked for the dup and could not find it. Can you? –  Sep 02 '16 at 03:32
  • @torazaburo https://stackoverflow.com/search?q=cast+name+string+window+%5Bjs%5D did work for me. But only because I cleaned up most of them myself :-) – Bergi Sep 02 '16 at 03:34

2 Answers2

0

oh but 'name' is reserved:

http://www.w3schools.com/js/js_reserved.asp

look under JavaScript Objects, Properties, and Methods

Niles Tanner
  • 3,911
  • 2
  • 17
  • 29
  • Thanks Niles , clearly i need to sleep more "thumbs Up*... – Hendrik Sep 02 '16 at 03:16
  • ha! Life of a programmer, still I think it's a fascinating find. – Niles Tanner Sep 02 '16 at 03:18
  • 4
    No, it's not "reserved", which means something specific. It's a built-in property on `window`. I would not refer people to w3schools. [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Window/name) is a much more reliable resource. –  Sep 02 '16 at 03:18
  • [Keywords on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords) as well. – Seth Sep 02 '16 at 03:20
0

If you debug, you will notice that name is already defined by the time you execute your code. It happens because global window context has name property which is string.
When you try to set ["a", "b", "c"] to this property, a browser converts it to a string, and it becomes "a,b,c". That's why when you iterate through it, you get characters.

console.log(name); // it already exists

var name = ["a", "b", "c"]; // assigns window.name property, becomes a string
var nameqwe = ["a", "b", "c"]; // creates local variable

console.log(name);
console.log(nameqwe);
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101