mortensen mortensen - 10 months ago 74
Javascript Question

Split empty string should return empty array

If I use


I get
instead of an empty array

How do I make sure that an empty string also returns an empty array?

Answer Source

You can use splice()


this example works for your problem:

function returnArr(str){
  return !str ? [] : str.split(',')

// ['1','2','3']