JavaScript beginner tutorial 18 - complex conditions

JavaScript beginner tutorial 18 – complex conditions



In this video I show you how to use the “AND”(&&) and “OR”(||) operators in JavaScript to check for multiple conditions in an if statement.

Don’t forget to subscribe:
http://www.youtube.com/user/QuentinWatt

For collaborations and business inquiries, please contact via Channel Pages: http://ChannelPages.com/QuentinWatt Social links:
————————————————————-
Add me on twitter:
http://www.twitter.com/QuentinWatt

facebook group:
https://www.facebook.com/quentin.watt
————————————————————-

source

36 comments

  1. Very helpful tutorials Quentin thanks. With your "if statement" tutorials, they are not real life scenarios. What if a user came to your website and typed in their age or name? How would you define a variable for infinite instances. I.E- not everyones name will be var firstName="Quentin"; I would like some more real life examples if i can be picky ^_^

  2. Actually, the extra parens aren't necessary in this case. Also, it's better to use === than ==.

    Good tutorials though.

    Btw, in case anyone's wondering, JavaScript doesn't have a built-in XOR operator like some other languages. In other words, there's nothing to compare if either the left side is true or the right side is true but NOT both. However, clever workarounds abound. Google is your friend!

  3. i noticed you just put "first_name" and didn't put in "var first_name" so when does this rule apply? when do i have to include var? thx a lot for these vids. i've learned a lot.

  4. firstName="Josh";
    lastName="Lyon";

    if((firstName=="Josh") && (lastName=="Lyon") ){
    document.write("Hello Josh Lyon");
    }
    else if (((firstName=="Josh") != (lastName=="Lyon") )){
    document.write("I do not know your full name but you can still enter")
    }
    else {
    document.write ("You cannot enter");
    }

    Just a little more advanced using some stuff from the last tutorials! Love your stuff friend, I shall be going through your Jquery and PHP after this. Already done Html and CSS!

  5. is there any certain situation that can only be done with nesting if or complex condition?? since, i think they have the same purpose.

    newbie here. correct me if i'm wrong.. thnks.. great lesson!

  6. For those that are reading, you can also set the && and || operators within the same parenthesis. E.g.

    if (firstName == 'Your name' || lastName == 'Your last name') {
    //do something
    } else {
    //do something else
    }

  7. There is also a way to use "or" (||) to greet, while using the users firstName lastName:

    firstName="Quentin";
    lastName=" Smith";

    if((firstName=="Quentin")||(lastName==" Watt")){
    document.write("Hello "+firstName+lastName);
    }

    __and__

    firstName="Sean";
    lastName=" Watt";

    if((firstName=="Quentin")||(lastName==" Watt")){
    document.write("Hello "+firstName+lastName);
    }

Comments are closed.