How To Pass Php Data To Javascript In WordPress With Wp_Localize_Script Use wp_localize_script() to expose PHP values such as the AJAX URL, a nonce or the site URL to your JS file.
How To Load A Javascript File Only On A Specific Page In WordPress Wrap wp_enqueue_script() in a conditional tag so the file loads only where it is needed.
How To Enqueue A Javascript File In WordPress Use wp_enqueue_script() inside the wp_enqueue_scripts hook to load your JS file the right way.