How should I test if a variable is defined?
if //variable is defined //do this else //do this
Use the typeof operator to check if a variable is defined or initialized, e.g. if (typeof a !== 'undefined') {} . If the the typeof operator doesn't return a string of "undefined" , then the variable is defined.
The __filename in the Node. js returns the filename of the code that is executed. It gives the absolute path of the code file.
Use the strict equality (===) operator to check if a variable is equal to true - myVar === true . The strict equality operator will return true if the variable is equal to true , otherwise it will return false .
if (typeof variable !== 'undefined') { // .. } else { // .. }
find more explanation here:
JavaScript isset() equivalent
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With