For example when trying to set the url of an Ajax request in jQuery, or setting the path to an icon image file on a Google Map.
Thankfully, if you’re using WordPress in the way it’s intended, there is a very simple way to make this possible.
wp_register_script('scripts', get_template_directory_uri().'/js/build/scripts.min.js', array('jquery'), '1.0.0', true);
'homeUrl' => esc_url(home_url()),
'templateDirUrl' => esc_url(get_template_directory_uri())
The wp_localize_script() function must be called after the script has been registered, but before it is enqueued.