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
Web Development Articles
Page 124 of 801
How to stop event propagation with inline onclick attribute in JavaScript?
Event propagation in JavaScript causes events to bubble up from child elements to their parents. When you have nested elements with click handlers, clicking a child element triggers both the child and parent handlers. The stopPropagation() method prevents this bubbling behavior. Understanding Event Propagation Let's see how event propagation works with nested elements: .parent-div { width: 300px; height: 150px; ...
Read MoreHow to test a value x against predicate function and returns fn(x) or x in JavaScript?
Testing a value x against a predicate function means checking if x meets a specific condition. If the condition is true, we apply a transformation function to x and return the result. Otherwise, we return the original value x unchanged. Syntax The basic syntax uses the ternary operator to test a value against a predicate function: let result = predicate(x) ? operation(x) : x; Where predicate(x) returns true/false, and operation(x) transforms the value when the predicate passes. Basic Example Here's a simple example that squares numbers greater than 5, otherwise returns the ...
Read MoreHow to uniquely identify computers visiting web site in JavaScript?
Whenever we create any application or website, we need to uniquely identify computers visiting the website. There are many benefits to uniquely identifying computers. For example, you can provide free trial services when a user visits your website for the first time from a new device. When they visit again, you can ask users to upgrade to premium or subscribe to your application. Here, we will use cookies to identify computers visiting our website. What are Cookies? Cookies allow developers to store user information in the browser. We can send data from the server to the ...
Read MoreHow to upload file without form using JavaScript?
Sometimes, developers need to upload files without creating traditional HTML forms. This is useful for drag-and-drop interfaces, single-click uploads, or when building custom file upload components. In this tutorial, we'll explore methods to upload files directly using JavaScript. Using FormData() Object and AJAX Request The FormData object allows us to store file data in key-value pairs, similar to form submissions. We can capture files from HTML input elements and send them to the server using AJAX without wrapping them in a form. Syntax let formData = new FormData(); formData.append("file", uploadedFile); $.ajax({ ...
Read MoreInsert a character after every n characters in JavaScript
Insertion of a specific character after every n characters in JavaScript is an easy-to-understand concept that gives us better understanding of JavaScript's string manipulation functions. Here n can be any whole number ranging from 1 to less than the length of the string. In this article, we'll explore different methods to insert a "-" character after every 5 characters in a string. Method 1: Using the slice() Method The slice() method extracts a portion of a string and returns a new string. It accepts two parameters: the starting index and the ending index (exclusive). let ...
Read MoreHandling Promise rejection with a catch while using await in JavaScript
In JavaScript, when working with async/await syntax, you need proper error handling for rejected promises. While Promise chains use .catch(), async/await functions require try-catch blocks to handle promise rejections. Basic Promise Creation Let's start with the fundamental syntax for creating promises: let testPromise = new Promise((resolve, reject) => { // perform some operation // resolve(value) for success // reject(error) for failure }); Example: Promise with .then() and .catch() Here's how traditional promise handling works with .then() and .catch() methods: ...
Read MoreHow to check if an object is empty using JavaScript?
In JavaScript, checking if an object is empty is a common requirement when working with dynamic data. An empty object contains no properties, and attempting operations on assumed non-empty objects can lead to unexpected behavior. For example, when fetching data from an API, you might receive an empty object if no results are found. Before processing this data, it's essential to verify whether the object contains any properties. We will explore three reliable methods to check if an object is empty in JavaScript. Using Object.keys() Method The Object.keys() method returns an array of all enumerable property ...
Read MoreHow to check for two timestamps for the same day in JavaScript?
The Date object is essential in JavaScript applications for creating and manipulating dates according to developer requirements. A common requirement is checking whether two timestamps represent the same day, which is useful for features like daily task tracking or date-based user activity validation. In this tutorial, we will learn three different approaches to check whether two timestamps are for the same day. This is particularly useful when you need to compare a user's last activity date with the current date. Using getFullYear(), getMonth(), and getDate() Methods The most straightforward approach is to compare the year, month, and ...
Read MoreHow to check whether or not a browser tab is currently active using JavaScript?
Users can open multiple tabs in the browser, and they can also check if any particular tab is currently active or not in the browser. For example, if you have given a proctored test, you can observe that it detects that tab is changed whenever you change the tab in the browser. So, there can be many applications and uses for checking whether the browser tab is active. This tutorial will teach us two methods to check whether the browser's tab is active. Using the onblur() and onfocus() methods of the window object The window object contains ...
Read MoreHow to check if a number evaluates to Infinity using JavaScript?
In JavaScript, when we divide any number by zero, we get the infinity value. Also, developers can make a mistake in writing mathematical expressions which evaluate to Infinity. Before performing any operation with the returned value from mathematical expressions, we need to check if the number value is finite. Here, we will learn three approaches to check if a number evaluates to Infinity using JavaScript. Comparing with Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY In JavaScript, the Number object contains POSITIVE_INFINITY and NEGATIVE_INFINITY properties that represent positive and negative infinity values. We can compare our numerical value with these properties to ...
Read More