Javascript Data Type Check Variable Existence

Description

Javascript Data Type Check Variable Existence


// true if variable exists, is a string, and has a length greater than zero

if (((typeof unknownVariable != "undefined") && (typeof unknownVariable
    .valueOf() == "string"))
    && (unknownVariable.length > 0)) {
  console.log("OK 1");
}

let unknownVariable;
if (((typeof unknownVariable != "undefined") && (typeof unknownVariable
    .valueOf() == "string"))
    && (unknownVariable.length > 0)) {
  console.log("OK 2");
}

unknownVariable = "test";
if (((typeof unknownVariable != "undefined") && (typeof unknownVariable
    .valueOf() == "string"))
    && (unknownVariable.length > 0)) {
  console.log("OK 3");
}



PreviousNext

Related