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
Javascript Articles
Page 11 of 534
How 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 MoreHow to check if a string is html or not using JavaScript?
When working with dynamic HTML content in JavaScript, it's crucial to validate HTML strings before inserting them into the DOM. Invalid HTML can break your webpage structure or cause rendering issues. This article explores two reliable methods to check if a string contains valid HTML using JavaScript. Using Regular Expression to Validate HTML Regular expressions provide a powerful way to match HTML patterns. We can create a regex pattern that identifies valid HTML tags with proper opening and closing structure. Syntax let regexForHTML = /]*>(.*?)/; let isValid = regexForHTML.test(string); Regex Pattern Explanation ...
Read MoreHow to check if the date is less than 1 hour ago using JavaScript?
In this tutorial, we will learn to find the difference between two dates and check if the difference is less than one hour. Sometimes, developers must play with the Date object and perform some operation every hour. So, this can be one approach that checks if a particular operation is performed before an hour, perform it again; otherwise, wait to complete the hour. Here, we will learn different approaches to check if the date is less than 1 hour ago using JavaScript. Use the getTime() method The getTime() method returns the total milliseconds for the date from ...
Read MoreHow to check if one date is between two dates in JavaScript?
In JavaScript, we can use the Date() object to create different timestamps. Also, we may be required to check if one date is between two using JavaScript. For example, we want to create a filter for orders in the eCommerce application based on the dates. So, we should be able to filter all orders between the two dates that users enter into the date input field. Another real-world use case of checking one date between two is in the banking application. For example, while developing the banking system application, developers need to create a filter allowing users to ...
Read MoreHow to check if the clicked element is a div or not in JavaScript?
When building interactive web applications, you often need to detect clicks on specific elements like divs. This is particularly useful for modals, dropdowns, or any clickable containers. This tutorial explores three effective methods to check if a clicked element is a div. Using the onClick Attribute The onClick attribute is the simplest way to detect clicks on a specific div element. You assign a function that executes when the div is clicked. Syntax This is a div! function clickFunction() { // perform operation on click ...
Read MoreHow to check input file is empty or not using JavaScript/jQuery?
In JavaScript, while working with the form elements, we need to validate the input fields and form elements when a user enters the value. In this tutorial, we will work with the file input. We will also learn to validate the file input. Sometimes, we may be required to check if the file is selected in the input field, then only enable the submit button; otherwise, disable the submit button. So, it will not allow users to submit the form or file without selecting it. Validate the file input using JavaScript In JavaScript, we can access the ...
Read MoreGet the relative timestamp difference between dates in JavaScript
Have you ever seen notifications on any website showing the time stamp? It shows something like "12 minutes ago", "2 days ago", "10 hours ago", etc. It is related to the timestamp difference between two dates or times. Also, some apps show that this device's last login was 22 hours ago. So, there are many uses to get the timestamp difference between two dates. In this tutorial, we will learn different approaches to get the relative timestamp difference between two dates. Using getTime() Method with Custom Algorithm In JavaScript, we can create a date object using ...
Read More