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?

overexchange
  • 15,768
  • 30
  • 152
  • 347

3 Answers3

0

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

Ashutosh Singh
  • 609
  • 6
  • 21
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).

Blaskovicz
  • 6,122
  • 7
  • 41
  • 50
  • Is this [code](https://github.com/shamhub/Javascript_Programming/blob/master/Topic8_DOM_Access/jstut.html) fine? – overexchange Dec 08 '15 at 04:26
  • 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. – Blaskovicz Dec 08 '15 at 04:31
  • 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). – Blaskovicz Dec 08 '15 at 04:32
  • Sorry, I meant "hyphen" as in "-" – Blaskovicz Dec 09 '15 at 03:14
  • Any example? For using bracket notation – overexchange Dec 09 '15 at 14:35
  • I'm basically saying you can write `window.prompt` when the object key doesn't have special characters. You'd have to do something like `window['foo-bar']` for the 'foo-bar' key because `window.foo-bar` isn't valid syntax. – Blaskovicz Dec 10 '15 at 14:13
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

brk
  • 48,835
  • 10
  • 56
  • 78