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

Common JavaScript errors

From: JavaScript Essential Training

Video: Common JavaScript errors

We may be creating web pages, but this is programming, and no matter how good we get, we'll spend less time writing code and more time figuring out why the code we wrote doesn't work. JavaScript is a forgiving language, but there are still common problems to be aware of. Now when you look at a page and it doesn't do what you were hoping, know that many JavaScript problems have failed silently. There will be no error message. They just won't work, although we can get some error message from some of our code, and let's cover those first. Make sure to have your JavaScript debugger, like Firebug, open, as the default configuration of most web browsers won't alert you to any JavaScript issues.

Common JavaScript errors

We may be creating web pages, but this is programming, and no matter how good we get, we'll spend less time writing code and more time figuring out why the code we wrote doesn't work. JavaScript is a forgiving language, but there are still common problems to be aware of. Now when you look at a page and it doesn't do what you were hoping, know that many JavaScript problems have failed silently. There will be no error message. They just won't work, although we can get some error message from some of our code, and let's cover those first. Make sure to have your JavaScript debugger, like Firebug, open, as the default configuration of most web browsers won't alert you to any JavaScript issues.

They just won't work. I'm going to refresh this page, and with Firebug open, it's giving me a problem here, and this is the situation, that first, and most importantly, we might have syntax errors. So I'm going to jump over to Aptana. I'll go to small JavaScript file that I'm using here with a few of the very common errors I want to show you. Most of it's commented out. I only have a few lines of active JavaScript here. First, and most importantly, you have syntax errors. Did you miss a closing brace, a closing bracket, or parentheses, or did you add an extra one, because these and many more will interfere with any of your JavaScript being understood.

One of the things you'll often see programmers do is when they are typing, say, if statements, they will actually type the opening and closing parentheses and the opening and closing curly braces before they then go back and start filling the things out, just to make sure that they are not messing it up. Now if I have code and there is a problem with this code here, over in Firebug it's actually saying that I've got a missing colon after property id. Now that message itself might not be very helpful, but when you do see messages that don't appear to make sense, you just need to start looking at your code.

Now, that's the message Firebug would give me. If you're using the Developer tools in any of the different browsers, they will often give you a different message. What I'm missing here is the closing parentheses after myFunction name. I am going to save that, go back in and refresh the page. Now I've got an unterminated string literal, and it is complaining about a particular line. If I even click the line, it would take me straight to that. What it appears to be there is another syntax error. I don't have the closing double quote. I'm going to go back and drop that one in, save that, back over into Firefox, refresh the page, and I actually get the normal log message, which was what I was hoping for.

But even after your syntax is correct and it's understood by the browser, there are still millions of mistakes that can happen. Now let's go through some common ones here. So I'm going to just comment this code out, because we've solved that one, and talk about possibly one of the most common situations here. You create a function called, in this case, myFunction, and you call it, or so you think. If I save this code, I go back into the browser and refresh it saying, myfunction is not defined.

This is one of the situations that will actually cause an error in any of the JavaScript debuggers. You're trying to call a function that doesn't exist, whether that is because of this situation just a typo. I've called it a lowercase f for function when I'm calling it, but I defined it with an uppercase F. Saving that, we refresh that page, and everything is working again. So calling a function that doesn't exist, a very common cause of JavaScript errors, and very common also, that you do a typo in some of the DOM methods, such as getElementByID.

In this case, this wouldn't work. If I save this, refresh this, I'll get a slightly different message: document.getElementByID is not a function. And you might be sitting and thinking, "Of course, it is. I've used it a hundred times." But of course, case sensitivity here, the D at the end of getElementByID should be lowercase. It's giving you a slightly different message here, because this is considered a method of an object rather than a function. Again, depending on what browser you're using, you may even see different messages for that.

The equivalent of that with your own code might be that you say create an array, such as I'm doing here on line 29, and I'm trying to call one of the object methods here called reverse, but I spelled it incorrectly, and we're going to get that equivalent there: myArray.revers is not a function--which is of course true. So comment goes back out and moving on. Now couple of situations we've talked about before. One is if you're creating a variable and calling document.getElementById, but there is any chance that actual part of the page hasn't loaded yet, you'll only have an undefined variable here.

This won't actually cause an error. Your code just won't work the way you expect it to, and that happens more and more as you get better with your code. You're not going to get those very obvious errors about functions not existing; it's just not going to work. Classic situation, of course, being this one. On line 41, I create a variable, a = 10, line 42 var b = 20, and then I have an if statement=, if (a = b), or so I think. Now in this case, I think a should never be equal to b, but I'm doing the assignment instead of equality.

So in my own mind I would never expect this message to come out, but of course this is exactly what's going to happen. Now notice there is no error message in Firebug, because technically the syntax is correct. But I'm getting this console.log, that "Something is wrong with the universe," because we're doing assignment instead of equality. We're setting a = b here, not checking a = b. A mistake that all programmers in C-based languages will run into once in a while. We'll change that, swap back over and I should have the message, "This is what I expect!" which of course it is.

Now again, moving on to a couple of things that would cause errors in other languages but don't officially cause errors in JavaScript, even though the behavior might not be what you want. Let's say this one, for example. I've defined a function called calculateSum. It takes three parameters--a, b and c-- and returns the value of those added together. Then a little later on I'm going to call it. I'm going to call calculateSum, passing in two parameters, and storing whatever comes back in my variable called result, and then writing out console.log(result).

Save that, jump back over into the browser. Now, this is not an error. All that I'm doing is writing out the contents of that variable, which is technically not a number. But I'm not getting any problem in the status bar. It's just an unexpected result, not an error, and those are two very different things. And of course this is because I am allowed to call a function passing in less parameters than it was defined with. What's happening here is that c would be accepted as undefined.

Then the code tries to add three together. If you are try and add an undefined variable together, that doesn't make sense, so the result is not a number. Now, there are of course a million ways your code can fail, but looking out for these basic situations will clear up a lot of those core issues.

Show transcript

This video is part of

Image for JavaScript Essential Training
JavaScript Essential Training

56 video lessons · 103984 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 preferencesfrom 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.