Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Conditional Logic in JavaScript - 06

Embed code for this video

Copy the code above to embed our video on your website/blog.

Close

Video format

Option selected may change based on video formats available and browser capability.

Close

Download

Right click “Save as…”

In this lesson, Bob demonstrates the use of the if ... else if statement, the ternary (or conditional) operator, and the case ... select statements. Along the way, we learn about the && and || operators as well as the Date object in JavaScript.

Tag:

Follow the Discussion

  • PeterNLPeterNL

    Nice psychedelic shirt Bob!

  • @PeterNL: Yeah, bad choice.  I'll never wear that one again on camera.  Sad

  • riclfriclf

    in my current language Objective-C the logical And operator in an If statement is a single &

    You showed it as a && which was a surprise. Is the single & never used? Thanks. Great videos!

    -Ric

  • @riclf: The single & is not used often -- at least not in the type of coding *I* do often ... 

    http://msdn.microsoft.com/en-us/library/sbf85k1c(v=vs.80).aspx

  • David HartiganDavid Hartigan

    Hi Bob,

    Thanks for making these videos! They are very easy to follow along with :) I did find the switch statement was a little bit unexplained at first but after re-watching it I've found it was rather simple.

    I am looking at making a start with writing Windows 8 Store Apps using Visual Studio Express 2012 and found that JavaScript is supported and with your help, easy to learn :)

    I have shared a link to these videos to a few of my friends.

    Thanks for the awesome videos, I look forward to watching the rest of this series!

  • @David Hartigan: Thanks David!  Glad they help!

  • If you look into the shirt long enough, you'll see a sailboat!

  • Congrats!  That is the single funniest comment we've received on any of these videos.  Smiley

    In retrospect, the wardrobe choice was ill-fated.

  • washerguy1washerguy1

    Hi Bob tanks again for the lessons. I had a problem when I pasted the code onto the validator page it spread out all over how do you word wrap it the way you did when you hit Ctrl c , Ctrl v into the validator.nu/sorry if this is a dumb question. John

  • @washerguy1: Hmm ... I don't think I did anything special.  Have you made sure to turn auto-wrap OFF in Notepad?  Are you using tabs or spaces?  Those are the only things I can think of that would affect it.

  • ThomasThomas

    @riclf In a c based languages (c, c++, java, objective-c, c#) & is the 'bitwise and' and && is the 'logical and', so condA & condB will also work, but the important difference is that there is no short circuit logic in the bitwise operators.

    @BobTaylor Actually most script languages support some notion of 'falsey' or 'truthey'. For example in python an empty list will evaluate as a false condition. In a sense so do most of the c based languages because a lot of things implicitly convert to bool. The messed up part with the '==' operator in javascript is that the conversion is done before the comparison.

  • KyleKyle

    I would first like to say thank you for putting these videos together. I am inching my way towards taking my first crack at some freelance work and these videos have helped out a lot with connecting the dots.

    I just wanted to see if you and/or anyone else was having an issue with the progress bar on the videos. It is not a big deal, but mine seem to stop progressing after multiple pause/restart actions.

  • @Kyle: Thanks Kyle.  As far as the player ... yeah, I noticed that too.  That would be good feedback for Microsoft -- and they don't monitor these threads like I do ... you may want to click the Feedback link (below, footer) and tell them so they can look into it.  Best wishes!

  • Robin GrossRobin Gross

    Okay, this is where it starts to not make sense to me. In the last example, where you're trying to determine the time of day and write a message to the html page -- I have a bunch of questions about this:
    1) Just to verify -- the Date function grabs the date and time stamp at any given moment, right? So the value that comes back from the Date function is put into the rightNow variable? Then you have to parse that value to find just the value of the hour, which is then put into the currentHour variable?
    2) Why do you do the "switch" on the value "true"? Why wouldn't you do the switch on currentHour? Isn't that what you're testing the value of?

  • Hi Robin ... sorry for the delay.

    re: Date() ... Yes:

    var rightNow = new Date();
    var currentHour = rightNow.getHours();

    In that code snippet, Date() grabs the current date and time in the form of a Date object.  See this for details, including reference about the getHours() (and other similar methods):

    https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date

    re: switch ... this is a trickier one ... honestly?  I probably should have chosen an if statement instead.  I think I wanted to explain the switch statement and show and advanced case.  As I wrote to you in our email exchange (that others didn't see) it's a stylistic choice.  

    In this advanced (perhaps even absurd) example, I'm testing the value of true to find a match.  It's a backwards notion ... again, "just because you can doesn't mean you should".  I probably shouldn't have done that if it confuses people ... but you obviously CAN do it.  In this case, prefer if.

  • Mr. Tabor,

       Been writing code for 6 years. First in high school and now in college. NEVER through to do a switch statement on "true". Blew my mind. Fantastic idea!

       These videos are amazing by the way. Been helping me out a ton with my capstone graduation project.

     

    -Zack

  • @zwarburg: Hi Zack, thanks ... I don't know if I would recommend that little trick other than to learn what is possible.  Smiley  I'd stick with an if..else.  Good luck on your project!

  • LutyaLutya

    Hi Bob,

    New to JS, thanks for doing these. I was using code academy before but this is more my pace. Two quick questions:

    1. Why do you use the break in the switch statement?
    2. I can't seem to get my code to work, even though it seems to match yours perfectly. Any suggections?

    <!DOCTYPE html>
    <html>
    <head>
    <title>6. Conditional Logic in JavaScript</title>
    <script type="text/javascript">
    var rightNow = new Date();
    var currentHour = rightNow.getHours();
    switch(true) {
    case (currentHour > 8 && currentHour < 12):
    document.write("Morning, sunshine.");
    break;
    case (currentHour === 12):
    document.write("Lunch Time!");
    break;
    case (currentHour > 12 && currentHour < 18):
    document.write("Fight the urge to nap!");
    break;
    deafault:
    document.write("You better go to bed soon!");
    break;
    }
    </script>
    </head>
    <body>
    <h1>6. Conditional Logic in JavaScript</h1>
    </body>
    </html>

  • @Lutya: You spelled default as deafault  Smiley

  • RachidRachid

    I just wanna thank you for your lessons, they make javascript look so easy and also wanna thank you for your time doing this. I feel like I knew javascript for along time.

  • @BobTabor
    Sir You are great... !
    I'm following along this series and I hope I'd surprise my class mates with my new JS , HTML5 & CSS3 Skills Smiley
    I'd even try to give MCP test for html5 !
    Thank You... your videos helps me alot...
    TBH the words arent enough to Thank You !

Remove this comment

Remove this thread

close

Comment on the Post

Already have a Channel 9 account? Please sign in