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.

#sidebar a { color:#fff; } #sidebar ul ul li { color: #DEF585; } #sidebar h2 { color: #fff; } #sidebar ul p, #sidebar ul select { color: #BEDDBE; } #backfly { background: url(images/golfBallWallPaper.jpg) left bottom fixed repeat-x #65a51d; }