is_page not working when loading javascript with add_action in functions.php

This code is working in my functions.php and it’s loaded properly:

add_action('wp_enqueue_scripts', 'js_custom', 50);
function js_custom() {
    wp_register_script( 'js_custom', get_template_directory_uri() . '/js/custom.js', false, null);
    wp_enqueue_script( 'js_custom' );
}

However, this is not working and not loaded:

if ( is_page(273) ) {
    add_action('wp_enqueue_scripts', 'js_custom', 50);
    function js_custom() {
        wp_register_script( 'js_custom', get_template_directory_uri() . '/js/custom.js', false, null);
        wp_enqueue_script( 'js_custom' );
    }
}

Why it’s not loaded? I am on the page with the id equal to 273.

Solutions Collecting From Web of "is_page not working when loading javascript with add_action in functions.php"

I had a similar issue once. I found that running the add_action call outside the conditional worked. So try this:

    function js_custom() {
        if ( is_page(273) ) {
            wp_register_script( 'js_custom', get_template_directory_uri() . '/js/custom.js', false, null);
            wp_enqueue_script( 'js_custom' );
        }
}
add_action('wp_enqueue_scripts', 'js_custom', 50);