update
I am trying to test to see whether a Javascript variable is undefined.
How to make an object that is missing a variable or undefined
before thank you if it is willing to help
this my code
var mydata = {
p001: "Product 1",
p002: "Product 2",
p003: "Product 3"
}
function test(e) {
if (mydata === undefined || mydata === null) {
document.getElementById("demo").innerHTML = "<em>No data</em>";
} else {
document.getElementById("demo").innerHTML = mydata[e];
}
}
var mydata = {
p001: "Product 1",
p002: "Product 2",
p003: "Product 3"
}
function test(e) {
if (mydata === undefined || mydata === null) {
document.getElementById("demo").innerHTML = "<em>No data</em>";
} else {
document.getElementById("demo").innerHTML = mydata[e];
}
}
#demo {
padding: 10px;
margin: 0;
border: 1px solid #2f9fd2;
border-radius: 4px;
background: #daf3ff;
}
<p>
<button onclick='test("p001");'>Product 1</button>
<button onclick='test("p002");'>Product 2</button>
<button onclick='test("p003");'>Product 3</button>
<button onclick='test("p10000");'>Test undefined</button>
</p>
<div id="demo"></div>
sorry my English is not good
Thank you
mydatais anObject, not anArray.