Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Error in function name allows code to run until error. Error in string "" causes everything to block. Why?

Hi

I discovered that if you type an error in the function name, like document.rite instead of document.write, then the code runs until the line of the error. If you have a faulty function name on line 4, the alerts and document.writes on line 1 to 3 will execute.

But if you make a mistake in the string formatting (forgetting an opening or closing quote ' or "), then none of the code runs. Even if the error is on line 4, line 1 to 3 will not execute, the console will just show you there is an error on line 4.

Why is this that the browser is able to execute the code if the error is in the function name, but not if you have not properly formatted a string with the opening and closing quotes "" or '').

Thanks!

Dave McFarland

Edit: This comment was posted relating to the following code challenge: https://teamtreehouse.com/library/javascript-basics/introducing-javascript/the-console-challenge

For some reason it didn't get included in the challenge's questions

1 Answer

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Great question Kenny Brijs

What you're looking at is two different types of errors: a syntax error and a run-time error. A syntax error is easy for a browser to spot -- a missing closing brace, mismatched quote marks, missing quote marks, for example. It's like a grammatical error in a sentence. Syntax errors are spotted as soon as the script is loaded, because the browser must "parse" the script in order to run it, the browser can find mistakes like that immediately.

Run-time errors, on the other hand, only come up as the program is running. For example, the document.rite() function you mentioned. This happens because the browser doesn't know immediately if that function (or method as it's technically called) exists or not until the program runs. For example, with this code

var a = 10;
console.log(b);

You won't get an error when this script loads, but you will when it runs. The browser doesn't know if the variable b exists until it tries to access that variable in memory. When it tries and can't find the variable you get a run-time error.

Likewise with document.rite(). It's technically possible for that method to exist. For example, you could create it in your script like this:

document.rite = function() {
   console.log("How about that, I just created the document.rite() method!");
};
document.rite();

Hope that helps.

Awesome, yes that sure helps!

Thanks for the fast, concise and easy to understand answer ;)