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.

Jeremy
  • 1
  • 85
  • 340
  • 366
digitale
  • 645
  • 4
  • 13
  • 4
    Possible duplicate of: http://stackoverflow.com/questions/6003884/how-do-i-check-for-null-values-in-javascript – Sam Ivichuk Nov 27 '15 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"`. – Sebastian Simon Nov 27 '15 at 18:48
  • 2
    @Sulthan He already is using `name == null`, which should match both `null` and `undefined`. – Jeremy Nov 27 '15 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? – digitale Nov 27 '15 at 18:52
  • @digitale I’ve just logged the ouput to the console to see what the actual return value is. The fact that you can’t distinguish the two options for `"null"` is why I haven’t posted an answer, but only a comment… I don’t know either… – Sebastian Simon Nov 27 '15 at 18:56
  • 4
    I think this is **not** a duplicate of http://stackoverflow.com/questions/6003884/how-do-i-check-for-null-values-in-javascript. 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.. – Luis González Nov 27 '15 at 18:59
  • `x == null` checks for `null` or `undefined`. – Rudie Nov 27 '15 at 21:22
  • possible duplicate of [Using the variable “name” doesn't work](http://stackoverflow.com/q/10523701/1048572) – Bergi May 11 '16 at 02:36

1 Answers1

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();
Jeremy
  • 1
  • 85
  • 340
  • 366
  • 1
    This makes sense. Thank you for mentioning the existence of the global variable, "name". – digitale Nov 27 '15 at 20:14
  • So this is the same issue as in [status.style is undefined](http://stackoverflow.com/q/31753711/4642212). Why haven’t I seen this immediately?! – Sebastian Simon Nov 28 '15 at 15:16