We launched a new IT training category! Check out the 140+ courses now.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Working with conditional code

From: JavaScript Essential Training

Video: Working with conditional code

Beyond the most basic JavaScript one-liners, we need to start asking questions, being able to have code that only runs under certain conditions rather than all the time, and we begin this with the classic if statement, found in any programming language, written in JavaScript, as in other C-based languages, using this format. We have the word if, and then we have a condition inside parentheses and then if that condition is true, we will execute whatever code is inside the braces here. Now a quick aside for the folks new to programming.

Working with conditional code

Beyond the most basic JavaScript one-liners, we need to start asking questions, being able to have code that only runs under certain conditions rather than all the time, and we begin this with the classic if statement, found in any programming language, written in JavaScript, as in other C-based languages, using this format. We have the word if, and then we have a condition inside parentheses and then if that condition is true, we will execute whatever code is inside the braces here. Now a quick aside for the folks new to programming.

Yes, in programming languages we are really picky about the symbols we use and about what we call them. So just to be very specific, when I say parentheses, I mean these; when I say brackets and I usually say square brackets, I mean these guys; and when I say braces, and I'll usually say curly braces, I mean these. Now, they serve similar purposes, to mark where something starts and where it finishes, but they are not interchangeable. Now all of these are always found in pairs. If you have an opening one, you will need a closing one.

It may be several lines later, but it needs to be there. So back to the if statement. So in this case, the parentheses are used to mark out whatever our condition is, and the curly braces area used to mark out what we'll do if that's true, which could be one JavaScript statement, could be a dozen, could be 100. Now whatever the condition is, whatever we are asking, whatever is in the parentheses here must evaluate as true or false. So if I have a variable called a and I want to see if it's less than 50, I don't care what it is.

I don't care if it's 49 or -5 million or 50.0001; all I want to know is is this true or is it false? Is a less than 50 or isn't it? Is b more than 20 or isn't it? All conditions must boil down to simply true or false. Now, if I have a variable called c and I want to check that it's equal to something, to check equality in JavaScript, as in other C-based languages, I can use the double equals sign. There is no spaces between them. Double equals is considered a single operator. Now heads up, even for the programmers, JavaScript has another way of checking equality, the triple equal sign.

We will come back and talk about equality again, but realize for now that if, in a condition, if you're asking if something is equal to something else, you will never use a single equal sign. As we saw with variables, a single equal sign is an assignment. It's a command. It sets a value, not checks a value. So we can use double equals or right now triple equals will do the same thing. Again, we'll come back to this one. And if I want to check that a variable is not equal another value, it's the exclamation mark and equal sign, in this case checking that the variable d is not equal to 100.

If that's true, we will execute our code. When you have several statements surrounded by these curly braces, this is what's referred to as a code block, and this is all the curly braces do. They group a section of code together. They don't have any other meaning than that. And as you will see, these blocks can be nested the same way, say, in HTML you can nest divs inside other divs. Now a quick word about curly braces. When reading other people's code, you're likely to see them occur in a couple of different styles.

I'm jumping across to Firefox with Firebug here where I have my little JavaScript window open, and I'm declaring a variable called amount and setting it equal to 500, and then I have my if statement here. If amount is less than 1000, that needs to be either true or false, then we will execute whatever's between the opening and closing curly braces. Now the opening curly brace right now is right after the condition, and that's probably the classic way that you'll see these for C and C++ programmers.

You might also see the braces on separate lines so that they line up. People get religious about brace styles. JavaScript of course does not care. This is regarded as insignificant white space, and it doesn't change the meaning of the code. The most commonly in most sample code you will download or you will find online, you will see it this way, immediately opening the brace after the condition but closing it on its own line. If I decide to run this, not surprisingly, I should have the pop-up box here saying it's less than 1000.

Now if you have an if statement and you only want to then execute one line of code, technically you don't need to put the braces in. You don't need to define a code block for one line, but it's good practice to always do it, because if you miss it off, it's really easy to mess things up if you decide to edit this code later, so always use the blocks. And we will always be using these code blocks, even if we only have one JavaScript statement within them.

So if this code is executed when the condition is true, well, what happens if it isn't? If we just leave it like this, nothing. It would skip over the if and begin on the next line. But if you wanted something else to happen, you can follow the if with an else statement and in this case have another code block that runs different code. If the first condition is true, we run the code in the first block; otherwise, we run the code in the second block. And you can even nest them inside each other and check a different condition. But don't nest too deep.

JavaScript can handle this quite fine, but lots of nested ifs, say even beyond two levels, is quite difficult to read and make sense of. If we do have to have more complex logic, we are going to start breaking things apart into their own functions.

Show transcript

This video is part of

Image for JavaScript Essential Training
JavaScript Essential Training

56 video lessons · 106891 viewers

Simon Allardice
Author

 
Expand all | Collapse all
  1. 3m 28s
    1. Welcome
      1m 1s
    2. What you should know
      1m 44s
    3. Using the exercise files
      43s
  2. 15m 41s
    1. Introduction to JavaScript
      8m 6s
    2. Creating your first JavaScript
      2m 13s
    3. Getting to know the tools and applications
      5m 22s
  3. 56m 8s
    1. Understanding the structure of JavaScript code
      7m 9s
    2. Where to write your JavaScript
      3m 56s
    3. Creating variables
      6m 21s
    4. Working with conditional code
      5m 44s
    5. Working with operators
      13m 28s
    6. Sending messages to the console
      2m 59s
    7. Working with loops
      8m 1s
    8. Creating functions
      8m 30s
  4. 36m 13s
    1. Working with arrays
      7m 57s
    2. Working with numbers
      6m 13s
    3. Working with strings
      8m 27s
    4. Working with dates
      5m 38s
    5. Working with objects
      7m 58s
  5. 9m 6s
    1. What is the DOM?
      5m 49s
    2. Working with nodes and elements
      3m 17s
  6. 25m 17s
    1. Accessing DOM elements
      11m 3s
    2. Changing DOM elements
      5m 42s
    3. Creating DOM elements
      8m 32s
  7. 24m 45s
    1. Introduction to JavaScript event handling
      8m 16s
    2. Working with onClick and onLoad events
      7m 36s
    3. Working with onBlur and onFocus events
      2m 36s
    4. Working with timers
      6m 17s
  8. 21m 41s
    1. Common JavaScript errors
      7m 14s
    2. Using Firebug
      4m 7s
    3. Going through a debugging session
      10m 20s
  9. 10m 13s
    1. Accessing form elements
      4m 20s
    2. Preventing a form from being submitted
      2m 36s
    3. Hiding and showing form sections
      3m 17s
  10. 9m 49s
    1. CSS and JavaScript
      3m 46s
    2. Removing and applying CSS classes
      2m 16s
    3. Changing inline styles
      3m 47s
  11. 19m 44s
    1. Understanding JavaScript style
      7m 39s
    2. Minifying your code
      4m 28s
    3. Using JavaScript code checkers
      7m 37s
  12. 22m 24s
    1. Introduction to JavaScript libraries
      3m 17s
    2. Linking to multiple JavaScript files
      2m 11s
    3. Introduction to jQuery
      12m 7s
    4. Using a content distribution network to deliver JavaScript files
      4m 49s
  13. 17m 35s
    1. JavaScript in HTML5
      9m 37s
    2. Using Modernizr
      3m 2s
    3. Using Strict Mode
      4m 56s
  14. 33m 3s
    1. Knowing the JavaScript to avoid
      6m 35s
    2. Introduction to regular expressions
      6m 56s
    3. Working with AJAX
      10m 44s
    4. Working with objects and prototypes
      8m 48s
  15. 21m 10s
    1. Example: Countdown
      8m 3s
    2. Example: Resize
      5m 47s
    3. Example: Accordion
      7m 20s
  16. 4m 58s
    1. Where to go from here
      4m 0s
    2. Goodbye
      58s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed JavaScript Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.