Log in from a computer to take this course

You'll need to log in from a computer to start Learn Intermediate JavaScript. But you can practice or keep up your coding streak with the Codecademy Go app. Download the app to get started.

apple storegoogle store

Creating an error doesn’t cause our program to stop — remember, an error must be thrown for it to halt the program.

To throw an error in JavaScript, we use the throw keyword like so:

throw Error('Something wrong happened'); // Error: Something wrong happened

When we use the throw keyword, the error is thrown and code after throw statement will not execute. Take for example:

throw Error('Something wrong happened'); // Error: Something wrong happened console.log('This will never run');

After throw Error('Something wrong happened'); is executed and the error object is thrown, the console.log() statement will not run (just like when a built-in JavaScript error was thrown!).

In the next lesson we will cover how to handle an error so that the rest of our code can run!



Use the throw keyword to throw an error with the message 'Username or password do not match'

After you clear this checkpoint, try adding some code after your throw statement. Also try to throw a new error and other data types to see what happens!

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?