Using WP functions inside javascript

I found out about wp_localize_scripts here and it solves my issue:

functions.php

$var=get_current_user_id();

$params = array(
  'max' => $var
);

wp_localize_script( 'myscript', 'MyScriptParams', $params );
wp_enqueue_script('myscript');

and in the .js file I can retrieve this variable with MyScriptParams.max.