0
var name = [];

function logger(names) {
  if (typeof names === "string") {
    console.log(names);
  } else if (typeof names === "object") {
    for (var name in names) {
      console.log(names.names);
    }
  }
}

function namesIn(namesData, functionality) {
  name.push(namesData);
  functionality(namesData);
}

namesIn({
  names: ["Michael", "Sally", "Billy", "Dinesh", "Zach"]
}, logger);

When I go to view my log in chrome I am greeted with this particular error message which is puzzling: "TypeError: name.push is not a function".

What is the problem?

CD..
  • 72,281
  • 25
  • 154
  • 163
Crayon
  • 39
  • 1
  • 6
  • 2
    Possible duplicate of [Using the variable "name" doesn't work with a JS object](http://stackoverflow.com/questions/10523701/using-the-variable-name-doesnt-work-with-a-js-object) — rename `name` from the first line of your code to something else and change it in `name.push` accordingly. – Sebastian Simon Jul 06 '16 at 16:43
  • It's to do with the variable named as `name` which is declared as a global variable. `name` is a restricted property of the `window` object that returns the `window`s name. Hence the error. See the question linked as an duplicate for a better explanation – Clyde Lobo Jul 06 '16 at 16:49

2 Answers2

0

Because you redeclared name in the for loop. And name now is a String, which doesn't support .push.

If you use let name instead of var name in your for-loop, it should work.

Sander Sluis
  • 160
  • 7
  • If you rename the variable within the for loop to something else, it still does not work – Clyde Lobo Jul 06 '16 at 16:40
  • I have fixed the error. Great point with the overridden var in names. However, .push does support strings! I'll put the fixed code in my answer. Thanks! – Crayon Jul 06 '16 at 16:40
  • @Crayon My point is that Array.prototype.push does support adding strings, but String.prototype.push does not exist. Anyway, glad you fixed it! – Sander Sluis Jul 06 '16 at 17:50
0
var names = [];
function logger(names){
  if( typeof names ==="string"){
         console.log(names);
    }
  else if( typeof names ==="object"){
    for(var name in names){
      console.log(names.names);
      }
    }
  }

 function namesIn(namesData, functionality){
    names.push(namesData);
    functionality(namesData);
  }

 namesIn({names:["Michael","Sally","Billy","Dinesh","Zach"]},logger);

The root of the issue was that in the for loop, I redeclared my name variable!

I also changed the variable name to names for readabilty.

Crayon
  • 39
  • 1
  • 6
  • interestingly, javascript treats var name = []; as string but var names = []; as object. if its type is string, push is not defined so get error, if its object, it works fine. – user669789 Jul 06 '16 at 16:51
  • @Sabarish Xufox has the correct explanation for the problem in the comments above. See [http://stackoverflow.com/questions/10523701/using-the-variable-name-doesnt-work-with-a-js-object](http://stackoverflow.com/questions/10523701/using-the-variable-name-doesnt-work-with-a-js-object) – user4040648 Jul 06 '16 at 17:04