split to array is sometimes dangerous [ 702 views ]
Goal: to notice the split function special feature
just split a delimited string to an array:
console.log('1,2,3'.split(',').length); // --> 3
but there is a strange behavior
console.log(''.split(',').length); // --> 1
we have an empty element! We need to check the source string is valid or not.
var src = ''; var arr = src ? src.split(',') : []; console.log(arr.length); // --> 0
The result is correct now. Just be careful.