jeff64 jeff64 - 5 months ago 23
Javascript Question

type error indexOf() is not a function in for loop

I saw there were other questions with a similar name, I looked through them and they didn't really have the same specific problem as mine.

For some reason the indexOf() is called not a function in my for loop. Tried testing outside the loop and it is just fine so unsure of the problem.

function sym(args) {
//new array
var newArray = [];

//loops through however many arguments are inputted
for(var i=0;i<arguments.length;i++) {
//loops through each argument array's items
for(var j=0;j<arguments[i].length;j++) {
//if the newArray array doesn't have the array item, it adds it to the new array, to avoid duplicates.
if(newArray.indexOf(arguments[i][j]) === -1) {
newArray = newArray.push(arguments[i][j]);
return newArray;

sym([1, 2, 3], [5, 2, 1, 4]);


newArray.push returns the length of the array that was pushed, not the array itself.

Remove the newArray = newArray.push(arguments[i][j]); and just make it newArray.push(arguments[i][j]); and your error should disappear.