Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Front End Technology Articles
Page 342 of 652
How do we use throw statement in JavaScript?
In this tutorial, we will learn to use the throw statement in JavaScript. The "throw" is a reserved keyword in JavaScript that allows programmers to create user-defined exceptions. Every programmer makes errors, and sometimes users enter invalid input that causes exceptions. While JavaScript has built-in exceptions like arithmetic and index out-of-bound exceptions, programmers often need to create custom exceptions using the throw statement inside try...catch blocks. Below, we have given different examples of using the throw keyword in JavaScript. Using the throw keyword to create exception In this section, we will learn to create simple user-defined ...
Read MoreWhat is onerror() Method in JavaScript?
The onerror event handler was the first feature to facilitate error handling in JavaScript. The error event is fired on the window object whenever an exception occurs on the page. Syntax window.onerror = function(message, source, lineno, colno, error) { // Handle the error return true; // Prevents default browser error handling }; Parameters The onerror handler receives five parameters: message: Error message source: URL of the script where error occurred lineno: Line ...
Read MoreHow to catch exceptions in JavaScript?
To catch exceptions in JavaScript, use try...catch...finally. JavaScript implements the try...catch...finally construct as well as the throw operator to handle exceptions. You can catch programmer-generated and runtime exceptions, but you cannot catch JavaScript syntax errors. Syntax try { // Code that may throw an exception } catch (error) { // Handle the exception } finally { // Optional - always executes } Example: Basic Exception Handling Here's a simple example demonstrating exception catching: ...
Read MoreHow to do basic form validation using JavaScript?
JavaScript provides a way to validate form data on the client's computer before sending it to the web server. This improves user experience by catching errors immediately and reduces server load. Basic form validation includes checking that all mandatory fields are filled in and that data meets specific format requirements. It requires looping through each field in the form and validating the data. Basic Validation Methods Form validation typically checks for: Empty required fields Data format (email, phone, zip code) Data length constraints ...
Read MoreHow can we debug JavaScript using Firefox?
In this tutorial, we will learn to debug JavaScript code using the Firefox web browser. Debugging helps us identify and fix unknown bugs and errors in our code. Even experienced programmers encounter situations where code works fine one day but suddenly crashes the next. Don't worry if your code crashes — once you learn debugging techniques, you can fix most bugs quickly and efficiently. Let's start by creating an example with a deliberate bug to demonstrate the debugging process. Example Code with Bug In this example, we'll create a simple string comparison tool that takes two input ...
Read MoreHow can we debug JavaScript in Internet Explorer?
Debugging JavaScript in Internet Explorer requires enabling built-in error reporting and using developer tools. While Internet Explorer is now legacy, understanding these techniques helps with maintaining older applications. Enabling Error Notifications By default, Internet Explorer shows a small error icon in the status bar when JavaScript errors occur. This icon is easily missed, so you can enable automatic error dialogs. To enable automatic error notifications: Open Internet Explorer Go to Tools → Internet Options → Advanced tab Check "Display a notification about every script error" Click OK to save changes ...
Read MoreHow to convert seconds to HH-MM-SS with JavaScript?
In this tutorial, we will learn to convert seconds to Hours, Minutes, and Seconds (HH:MM:SS) format in JavaScript. Converting seconds to a readable time format is essential for displaying durations, timers, or elapsed time in web applications. JavaScript provides several approaches to achieve this conversion. Using the toISOString() Method The toISOString() method is one of the simplest approaches to convert seconds into the standard HH:MM:SS format. This method creates a Date object and extracts the time portion. Syntax var calculatedTime = new Date(null); calculatedTime.setSeconds(seconds); var newTime = calculatedTime.toISOString().substr(11, 8); Example The ...
Read MoreWhat is oncontextmenu event in JavaScript?
The oncontextmenu event in JavaScript triggers when a user right-clicks on an element, causing the context menu to appear. This event allows you to handle right-click interactions and can be used to customize or prevent the default context menu behavior. Syntax element.oncontextmenu = function() { // Handle right-click }; // Or using addEventListener element.addEventListener('contextmenu', function(event) { // Handle right-click }); Example: Basic Context Menu Event Here's how to handle the oncontextmenu event when a user right-clicks on an element: ...
Read MoreWhat is onmouseleave event in JavaScript?
The onmouseleave event triggers when the mouse pointer moves out of an element. It fires only when the mouse completely leaves the element boundary. Syntax element.onmouseleave = function() { // Code to execute when mouse leaves }; // Or using addEventListener element.addEventListener('mouseleave', function() { // Code to execute }); Example: Basic Mouse Leave Event Here's how to implement the onmouseleave event: function sayHello() { ...
Read MoreWhat is onkeydown event in JavaScript?
The onkeydown event in JavaScript triggers when a user presses down a key on the keyboard. This event occurs before the key is released and before the character appears in the input field, making it useful for intercepting keystrokes and implementing custom keyboard behaviors. Syntax element.onkeydown = function(event) { // Handle keydown event }; // Or using addEventListener element.addEventListener('keydown', function(event) { // Handle keydown event }); Basic Example Onkeydown Example ...
Read More