3

Consider this code:

var name = prompt("What is your name", "Anonymous");
if (name == null) {
  alert("detected null");
  name = "Anonymous";
}
alert("Hello " + name);

It is my understanding that clicking [Cancel] or pressing the [Escape] key will cause the JavaScript window.prompt(text, [default]) function to return null (or perhaps undefined in older versions of IE)

When I execute the above code in Firefox, the expected prompt appears. However, when I press [Escape], I never see the "detected null" message (and the name variable is not set to "Anonymous". It is as if the name variable is not being set to null. Interestingly, the last alert displays "Hello null".

I've tried swapping out the name == null check with name === null with the same behavior.

How does one detect null in JavaScript?

Please note: I am really trying to detect null, not an empty string.

8
  • 4
    Possible duplicate of: stackoverflow.com/questions/6003884/… Commented Nov 27, 2015 at 18:44
  • 1
    The return value of prompt seems to always be a string. If you press the escape key, you’ll get the string "null". Commented Nov 27, 2015 at 18:48
  • 2
    @Sulthan He already is using name == null, which should match both null and undefined. Commented Nov 27, 2015 at 18:48
  • @Xufox, I have not tested your claim yet. But if it is true, how does one distinguish between the user pressing [Escape] / clicking [Cancel] and a user who actually entered the string "null" as their input? Commented Nov 27, 2015 at 18:52
  • 4
    I think this is not a duplicate of stackoverflow.com/questions/6003884/…. The main difference is that in this case the null value is produced by a prompt function, and I didn't know that to check if I have pressed 'cancel' I have to compare the value with "null". So in this case there is not a null value, It's a string with the "null" value.. Commented Nov 27, 2015 at 18:59

1 Answer 1

4

The prompt() function returns null when the user presses escape, as described in the Web application APIs section of the latest HTML 5 Working Draft:

  1. If the user aborts, then return null; otherwise, return the string that the user responded with.

You just need to check that result === null or result == null, as you have done.

The behaviour you're encountering doesn't have to do with prompt(). It's because you're trying to use the variable name in a global scope. You aren't actually declaring a new variable. You're reusing the global window.name property, which is automatically converted to a string:

var nameNumber = 21;
var name = 21;
document.write([typeof nameNumber, typeof name]);  // number,string

Your code will work correctly if you use a different variable name, or if you wrap it in a function so it isn't colliding with the global variable.

function main() {
  var name = prompt("What is your name", "Anonymous");
  if (name == null) {
    alert("detected null");
    name = "Anonymous";
  }
  alert("Hello " + name);
} 

main();

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

2 Comments

This makes sense. Thank you for mentioning the existence of the global variable, "name".
So this is the same issue as in status.style is undefined. Why haven’t I seen this immediately?!

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.