function my_function() { var num_num = 1; } $(document).ready(function () { my_function(); alert(num_num); }); 

In the log writes Uncaught ReferenceError: num_num is not defined . How can I pass the variable num_num to ready(function () ?

  • one
    And why, for example, do not return num_num as a result of the function execution? There would be no global variables. - Regent

2 answers 2

 var num_num = 0; function my_function() { num_num = 1; } $(document).ready(function () { my_function(); alert(num_num); }); 

Although the option that Regent offers is more correct:

 function my_function() { var num_num = 1; return num_num; } $(document).ready(function () { alert(my_function()); }); 
  • Thank !!!!!!! - Alyoshka Lavrushka
  • I tried to do so. In the function: var right = -Math.floor((window_width - text_page_1_width) * (23 / 30)); return right; var right = -Math.floor((window_width - text_page_1_width) * (23 / 30)); return right; . In the main function: var new_right = right; alert(new_right); var new_right = right; alert(new_right); . What is wrong here? - Alyoshka Lavrushka
  • Well, maybe I do not know how to properly call a variable? Just through the alert (as you) everything works. And I can't get something to change - Alyoshka Lavrushka
  • var new_right = my_function (); - Anton Komyshan
  • Got it. Thank. - Alyoshka Lavrushka

Declare a variable outside the function, not inside.