0

Below is the code,

<p id="sayHello"></p>
<script type="text/javascript">
    var yourName = window['prompt']("What is your name?");

    if (yourName != null) {
        window['document']['getElementById']("sayHello").innerHTML = "Hello " + yourName;
    } else {
        window['alert']("Please enter your name next time");
    }
</script>

for which, else block need to get executed based on the input given in prompt.

What should be the input in prompt box to test null value of primitive type Null?

7
  • 2
    == null / != null is totally fine. What exactly is your question? Doesn't that code work? Commented Dec 8, 2015 at 3:54
  • @Bergi Trying to know, for what input, null check will be satisfies. as you said, by pressing cancel button. Commented Dec 8, 2015 at 3:56
  • @AshwinGupta I said Null primitive value null Commented Dec 8, 2015 at 3:58
  • 1
    @overexchange: Yes, prompt returns the null value when being cancelled, and comparing with null totally tests for that condition. Commented Dec 8, 2015 at 3:59
  • @Bergi Thank you for the answer. Commented Dec 8, 2015 at 3:59

3 Answers 3

0

When you click cancel on the prompt box the else block will get executed.

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

Comments

0

Per the MDN window.prompt docs:

If the user clicks OK without entering any text, an empty string is returned.

So really you want to check if (yourName !== null && yourName !== "") since the prompt is really returning the empty string (thus causing your else clause to be executed incorrectly since it's passing the not null check).

6 Comments

Is this code fine?
Yes, that code looks sound. It first checks if the name is empty (if so, say name is empty), then checks if it's null (if not, use the name), otherwise it says a different warning. Note that for most cases in javascript you want to use a triple-equal comparision (=== or !==) so that type checks hold as well.
You can test code in your browser's console btw. Just hit f12 and you can run the exact code. I would also recommend omitting the verbose object access when possible (eg: use window.prompt rather than window['prompt'] since you only need the second form when there are special characters like hympen).
Sorry, I meant "hyphen" as in "-"
Any example? For using bracket notation
|
0

I think you actualy looking for empty string.Also null is a primitive value & null represent an "empty" value, that is no object value is present. So to check null we can use

if(somVar === null && typeof somVar ==='object')

So you can arrange you code as

var yourName = window['prompt']("What is your name?");
if (yourName === null & typeof(yourName) ==='object') {
       alert("Please enter your name next time");
    } else {
       document.getElementById("sayHello").innerHTML = "Hello " + yourName; 
    }

Also note this will ONLY test for null and will not pass for "",undefined,false,0 & NaN. Beside is there any reason to use

window['document']['getElementById']("sayHello")

when it can be done like this

  document.getElementById("sayHello").innerHTML 

If you are checking for empty string , then you also have to validate that input is not empty

DEMO

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.