2

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

2
  • Your code in the snippet works totally fine. What exactly are you trying to ask? Commented Jul 16, 2018 at 11:13
  • mydata is an Object, not an Array. Commented Jul 16, 2018 at 17:48

3 Answers 3

1

I think this is what you are looking for.

Just check if there is no value for the key e in mydata.

By the way, there is no array here. Using the proper terms is important. You work with an object and you want to check if a property of it is defined or not.

var mydata = {
	p001:"Product 1",
	p002:"Product 2",
	p003:"Product 3"
}

function test(e) {
  if (!mydata[e]) {
    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>

Sign up to request clarification or add additional context in comments.

Comments

0

You could extend the check for undefined values.

if (mydata === undefined || mydata === null || mydata[e] === undefined) {
                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^

var mydata = {
	p001:"Product 1",
	p002:"Product 2",
	p003:"Product 3"
}

function test(e){
   if (mydata === undefined || mydata === null || mydata[e] === undefined) {
    	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>

Comments

0

I believe there is no need of doing two check for the same thing like this:

if (mydata === undefined || mydata === null) {
        document.getElementById("demo").innerHTML = "<em>No data</em>"; 
    } 

do this instead with a == operator cause basically null == undefined so this will eventually save you as === checks for an exact match but == will give you true for both null and undefined as it does the type coercion for you:

if (mydata == null){
    // your code here.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.