xxxxxxxxxx
if (typeof variable === "undefined"){
//Variable is not defined
console.log("Variable \"variable\" is not defined");
}
xxxxxxxxxx
let id;
if(typeof id === 'undefined') {
console.log("id is undefined...");
}
xxxxxxxxxx
if ( typeof query !== 'undefined' && query )
{
//do stuff if query is defined and not null
}
else
{
}
xxxxxxxxxx
let myVar;
if (myVar === undefined){}
//!! Note: myVar == undefined would also check wether myVar is null
//alternatively
if (typeof myVar === 'undefined'){ }
xxxxxxxxxx
const obj =
{
"name": "John Doe",
"age": 39,
"Street": "Hauptstraße 5"
}
// street is undefined (its uppercase)
var { name: fullname, age, street } = obj;
// You need an array [...]
// if you will check one variable
TestUndef([age]);
// or more
TestUndef([fullname, street, age]);
console.log("All Ok");
function TestUndef(what) {
for (var that of what) {
if (typeof (that) == "undefined") {
throw new Error(`UNDEFINDED OBJECTS!`);
}
}
}
// no write "street" in line 5 lowercase, then its all ok.