is_customize_preview() like function to check if Customizer “Live Preview” in JavaScript

Is there a predefined JavaScript function that can be use to check whether the site is being previewed in the Customizer which returns true if it is or false if it’s not, just like what is_customize_preview() PHP function doing?

if (is_customize_preview()) {
    // Script for Customizer Live Preview
}

If there’s no such predefined function then how can I check if the site is being previewed in the Customizer, in JavaScript?

Solutions Collecting From Web of "is_customize_preview() like function to check if Customizer “Live Preview” in JavaScript"

you can just check for the customize object, but if you absolutely need a conditional statement to determine whatever it is you need, you can do something like this:

jQuery( function( $ ) {

    // set var
    var in_customizer = false;

    // check for wp.customize return boolean
    if ( typeof wp !== 'undefined' ) {
        in_customizer =  typeof wp.customize !== 'undefined' ? true : false;
    }

    // if you're in the customizer do this
    if ( in_customizer ) {
        console.log('in customizer');

    // if you're not in the customizer do this
    } else {
        console.log('not in customizer');
    }

});