this question has answer here:
i little guidance here. using js bin this. whole issue here : "name" when used var , inside 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 var username, yields correct result,..i cant find reference 'name' being reserved word in js, if clarify me, 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"
if debug, notice name
defined time execute code. happens because global window
context has name
property string.
when try set ["a", "b", "c"]
property, browser converts string, , becomes "a,b,c"
. that's why when iterate through it, characters.
console.log(name); // exists var name = ["a", "b", "c"]; // assigns window.name property, becomes string var nameqwe = ["a", "b", "c"]; // creates local variable console.log(name); console.log(nameqwe);
Comments
Post a Comment