A runtime error in JavaScript is a problem that occurs while your code is running. When this happens, the code stops working correctly. Some runtime errors are built into JavaScript and come with a name and message that describe the problem. Any code written after the error won't run.
// This will create and show an error message saying 'Something went wrong' throw Error('Something went wrong'); // The code below won't run because of the error console.log('This message won’t be shown');
The throw keyword in JavaScript is used to create and show an error. When you use throw with an Error() function or object, it stops the code from running any further.
throw Error('Your password is too weak.'); // This will stop the code and show 'Your password is too weak.'
The Error() function in JavaScript creates an error message. You can add a custom message to explain what went wrong. The error won’t stop your code unless you use the throw keyword to raise it.
// Using try...catch to handle errors try { throw Error('This error will be handled'); } catch (e) { console.log(e.message); // This will show the error message } // Handling a built-in error const fixedValue = 'Cannot be changed'; try { fixedValue = 'New value'; // This will throw a TypeError } catch (e) { console.log('An error occurred!'); // This message will be shown } console.log('Code continues after handling the error');
The try...catch statement in JavaScript lets you catch and handle errors. Code that might cause an error is placed inside the try block. If an error happens, the catch block will run, letting you decide what to do next.
// Example of catching a ReferenceError in JavaScript let firstName = "John"; try { console.log(firstName + lastName); // lastName is not defined, causing a ReferenceError } catch (error) { console.log('Caught an error:', error.message); // This will show: 'Caught an error: lastName is not defined' }
A ReferenceError happens when you try to use a variable that hasn't been declared yet. It's like calling someone who doesn't exist in your contacts.
let age = 25; console.log(age + years); // This will cause a ReferenceError because 'years' is not defined.
A SyntaxError occurs when there is a typo or mistake in your code, making it impossible for the browser to understand it.
try { eval('function name( { '); // Missing closing parenthesis causes a SyntaxError } catch (e) { console.log('Caught a SyntaxError:', e.message); // This will show: 'Caught a SyntaxError: Unexpected token {' }
A TypeError is thrown when you try to do something with a value that is of the wrong type. For example, trying to add a number to a string can cause this error.
let number = 10; let text = 'Ten'; try { console.log(number + text); // This will cause a TypeError because you can't add a number to a string } catch (e) { console.log('Caught a TypeError:', e.message); // This will show: 'Caught a TypeError: number + text is not a valid operation' }
The MDN JavaScript error documentation is a great resource for learning about different types of errors, what causes them, and how to fix them. It's useful when you encounter an error that you're not familiar with.
A stack trace is a list of the steps your code took before it ran into an error. It's helpful for finding out where things went wrong so you can fix the problem.
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!