If I use
''.split(',')
I get [''] instead of an empty array [].
How do I make sure that an empty string always returns me an empty array?
Just do a simple check if the string is falsy before calling split:
function returnArr(str){
return !str ? [] : str.split(',')
}
returnArr('1,2,3')
// ['1','2','3']
returnArr('')
//[]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With