$(function(){ var state = aux_state('aux1'); alert(state); // LINE 1 $('#aux1').switchbutton({ checked: aux_state('aux1'), onChange: function(checked){ $.get(host +'/set_aux/aux1/'+ checked, function(state){ // ... }); } }); }); function aux_state(aux){ $.get(host +'/get_aux/' + aux, function(state){ alert(state); // LINE 2 return state; }); }