Shadowing Posted November 30, 2011 Share Posted November 30, 2011 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 More sharing options...
justsomeguy Posted November 30, 2011 Share Posted November 30, 2011 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 More sharing options...
Shadowing Posted November 30, 2011 Author Share Posted November 30, 2011 thanks I really appreciate the help Ive seen other if commands saying if 3 is < 10 then this and that happendswhy is this if command using "not" is it just only for when using the show_prompt function? Link to comment Share on other sites More sharing options...
justsomeguy Posted November 30, 2011 Share Posted November 30, 2011 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 More sharing options...
Shadowing Posted November 30, 2011 Author Share Posted November 30, 2011 Thanks alot that makes alot of sense. you explained that perfectly! I'm so glad I asked about that haha. Talk about understanding something backwards lol Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.