0

I am very new to JS and am trying to learn to remove the commas from an array i.e. (Object.getOwnPropertyNames(window))

When I try to run the split I keep getting "Uncaught TypeError: myVar.split is not a function"

//my array
myVar = (Object.getOwnPropertyNames(window));
document.writeln(myVar)

//to remove commas
var newVar = myVar.split(',');
document.writeln(newVar);

https://jsfiddle.net/8ywq0dt3/1

Hoping someone can help a novice out....

Thanks Mike

3
  • 3
    Arrays don't have commas, nor do they have a split() method. If you mean you want to format the array as a string without commas, you probably want myVar.join("some delimiter that's not a comma"). See Array.prototype.join() Commented Jul 25, 2022 at 0:21
  • Thanks @Phil that seemed to work... I think I am confused however, so the commas in the output aren't in the array, they are just a to make it more readable to humans? Commented Jul 25, 2022 at 0:27
  • All JS objects have a toString() method that is invoked when you try to use them in a string context. The behaviour of Array.prototype.toString() is well documented Commented Jul 25, 2022 at 0:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.