JavaScript проверка, если не определено
if (typeof myVariable === 'undefined'){
//myVariable is undefined
}
Grepper
if (typeof myVariable === 'undefined'){
//myVariable is undefined
}
if (typeof myVar !== "undefined") {
console.log("myVar is DEFINED");
}
let myVar;
if (myVar === undefined){}
//!! Note: myVar == undefined would also check wether myVar is null
//alternatively
if (typeof myVar === 'undefined'){ }
if(typeof comment === 'undefined') {
alert('Variable "comment" is undefined.');
} else if(comment === null){
alert('Variable "comment" is null.');
}
let id;
if(typeof id === 'undefined') {
console.log("id is undefined...");
}
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.