If you declare a variable outside the function, then assign it a value inside the function, it should be available elsewhere. Until you are sure that a value will be assigned. If you are not sure, you can assign a default value:
$(function() { var bwr_w; // or 'var bwr_w = default_value;' function init() { bwr_w = $(window).width(); } init(); $('#button').click(function() { alert('The Browser Height is' + bwr_w); }); });
source share