JavaScript check if variable exists (is defined/initialized)

You want the typeof operator. Specifically:

if (typeof variable !== 'undefined') {
    // the variable is defined
}

Leave a Comment