0

I have been writing some code pertaining to random number generating, and I would add the user's name to the sentence that was produced by the script.

The user inputs their answer with a prompt. I wanted to know how to check for null, so I tested this with the answer given by multiple sources on Stack Overflow. However, it does not work for null. Curiously, it does work when null is inputted as a string: "null". I have read over and over again that null is not a string, so why would that work?
My code for the test is below:

<!DOCTYPE html>
<html>
  <head>
    <title>Check</title>
    <script>
      var name = prompt("What's up?");
      function check() {
        if (name === null) {
          document.write("null");
        }
      }
    </script>
  </head>
  <body id="body" onload="check()">
  </body>
</html>
  • 1
    I see the problem now ... change `name` to something else, because `name` is a window property that will mess you up!!! – Jaromanda X Jan 14 '17 at 02:33

2 Answers2

2

When someone types the input, it is received by the script as a String. So if someone types null it translates to "null", i.e. a String with value null. So when you try to evaluate it to the value null it does not work. However if you evaluate it to the String "null" it will match.

EDIT: If you do not want to allow user to enter null or any other malicious values, create a list of such strings and check against them. Treat them as string values

The function does execute when you hit Cancel aswell, but since you are cancelling the operation, the value of name is set as "null". That is how the prompt function works. https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt

poushy
  • 1,114
  • 11
  • 17
  • To add to poushy's answer, you need to check if the string is empty rather than `null` - https://jsfiddle.net/orrLqtfm/. – Max Sindwani Jan 14 '17 at 01:43
  • 1
    you should still check for `null` to test if the `cancel` button was pressed – Jaromanda X Jan 14 '17 at 01:56
  • If you do not want to allow user to enter null or any other malicious values, create a list of such strings and check against them. Treat them as string values. – poushy Jan 14 '17 at 02:02
  • @JaromandaX I think you misinterpreted my original question. I **am** testing for the cancel button. I am sorry if I did not make that clear. When I run the code I posted above, and hit the cancel button, the function does not execute. – MCBlastoise Jan 14 '17 at 02:25
  • @MCBlastoise I wasn't referring to your question, which is why the comment is in this answer – Jaromanda X Jan 14 '17 at 02:29
  • @JaromandaX Oh sorry. I'm kinda new to this. – MCBlastoise Jan 14 '17 at 02:31
  • And to @MaxSindwani, it does work when I test for an empty string. It just doesn't work for the cancel button. – MCBlastoise Jan 14 '17 at 02:32
  • No probs, see comment to your question ... you've picked a variable name that will be problematic ... a quirk of the browser ... `name` is always a string because `name` is a predefined property of `window` that has will mess you up, like you've seen – Jaromanda X Jan 14 '17 at 02:35
  • @MCBlastoise Ah, then yeah you'll need to check for null specifically if you want to differentiate between submit and cancel - https://jsfiddle.net/msindwan/orrLqtfm/3/ – Max Sindwani Jan 14 '17 at 02:37
  • in this case, `name` will never be `null`, `Name` would, so would `nAme` but not `name` - quirk of the global (window) object – Jaromanda X Jan 14 '17 at 02:44
  • Thanks so much, @JaromandaX! This was the reason it kept failing. I'm new to Javascript, and so I didn't know about name being a special keyword. – MCBlastoise Jan 14 '17 at 02:55
  • @JaromandaX - The function does execute when you hit Cancel aswell, but since you are cancelling the operation, the value of name is set as "null". That is how the prompt function works. https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt – poushy Jan 14 '17 at 02:57
  • @poushy - the `check` function **in the question** will execute regardless ... the issue is with the choice of variable name as I've stated – Jaromanda X Jan 14 '17 at 03:11
  • Yes you are right @JaromandaX - the naming messes with the fact that it delegates to a string. – poushy Jan 14 '17 at 03:16
1

The issue is purely the choice of variable name ... i.e. name

the code with (almost) any other variable name will work "as expected"

var name = ... in the global scope is essentially the same as window.name = ...

Now, window.name is defined as the name of the window ... this is usually an empty string. if you window.open a window, giving a name, the window.name will be that value in the new window

Now, assigning null to window.name will actually result in window.name == 'null' because window.name can not be a non string type. In fact assigning any non-string value to window.name will result in window.name having the ".toString()" value

try

window.name = true; // window.name will be the string "true";
window.name = {key: "value"}; // window.name will be the string "[object Object]";

etc

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87