syntax - Why do I see javascript arrays getting created with string.split()? -


I see such code across the web

  var days = "Monday Tuesdays Wednesday Thursday Friday Saturday Sunday ".split (" ");  

Why do so

  var days = ["Monday", "Tuesday", "Wednesday", "Thursday", instead of "Friday" , "Saturday Sunday"];  

I do not think there is nothing with idleness or ignorance. This jQuery is out of 1.4.2

  Sahara: "altKey attrChange attrName bubbles button cancelable charCode clientX clientY ctrlKey currentTarget data descriptioned event element handler key codecod layerx learer meta newoffice offset x offset y original Goal Page X Page Y PARWorldX Related Noida Related Goal ScreenX Skinny Shift's Element Goal for White Element View Wheel Delta ".split (" ")  

do it everywhere Areas are.

I think this is because you do not have to quote each string in the array and separate it . Similarly, in Perl, many people qw (abcdefg) ('a', 'b', 'c', 'd', 'e', ​​'f' , 'G') . Therefore the profit is two times:

  1. Fast and easy to write and modify (can be clearly debated)
  2. It's small, so you remove some bandwidth.

View bit size:

  var days = "Monday Tuesday Wednesday Thursday Friday Saturday Sunday" .split (""); // 81 characters  

versus

  var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday Sunday"]; // 91 characters  

Comments