Jump to content

Need Help On Prompt Box


Shadowing

Recommended Posts

I got stuck on the java script tutor on prompt boxes. Wondering if someone could explain it to me a little better http://www.w3schools...me=tryjs_prompt I dont understand this if command if (name!=null && name!="") { document.write("Hello " + name + "! How are you today?"); I understand what the command is doing but I dont understand why it does it. The way im reading this is. If name equals not invalid "meaning if the name field is blank" and the name is there then it writes which wouldnt make any sense obviously since the name field would have to be blank and filled out at the same exact time. Thats why i dont understand why it works. I just dont see how it means that a name has to be there and cant be blank appreciate any help in me trying to understand this thanks

Link to comment
Share on other sites

It writes the name if the name variable is both not null and not empty. A null value means the variable doesn't have a value, which may not apply in this case. Maybe older browsers would set the variable to null if you just close the prompt box without doing anything else. You can read the if statement as "if name is not null and name is not empty".

Link to comment
Share on other sites

No, the not operator is one of the most frequently used operators, right up there with ==. This uses not because it wants to print the message in all cases but a few. It would use == instead of not if it wanted to show the message in only a few cases. e.g.: if (name == 'Steve' || name == 'John') With that, it would only print the message if the name matched one of those.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...