split - Can someone explain to me what the square brackets do here in this jQuery code? -


$(document).ready(function(){ $('#switcher-default').addclass('selected'); $('#switcher button').on('click', function(){      var bodyclass = this.id.split('-')[1];      $('body').removeclass().addclass(bodyclass);     $('#switcher button').removeclass('selected');     $(this).addclass('selected');     console.log(bodyclass); }); });  //////////////html code/////////////  <div id="switcher" class="switcher">     <h3>style switcher</h3>     <button id="switcher-default">         default     </button>     <button id="switcher-narrow">         narrow column     </button>     <button id="switcher-large">         large print     </button> </div> 

this code works , can see 1 in square brackets confusing me. mean split id "-" used , take first part? don't know? apreaciate help.

it means split string identified this.id, results in array. use value index 1 array set variable bodyclass.

note in many settings when not 1 generating data, may unsafe assume resulting array has particular number of elements.


Comments