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
Articles by Yaswanth Varma
Page 23 of 31
How do you display JavaScript datetime in 12hour AM/PM format?
The most practical way to display datetime for efficient time analysis is in a 12-hour AM/PM format. This method clarifies the distinction between morning and evening, making times like "2:30 PM" more intuitive than "14:30" in 24-hour format. This article will explain various methods for displaying JavaScript datetime in 12-hour AM/PM format. Using the toLocaleString() Method The toLocaleString() method returns a formatted date string according to the specified locale and options. It's the most straightforward approach for 12-hour format conversion. Syntax Date.toLocaleString(locales, options) Parameters locales − Language/region ...
Read MoreSubstitute random items in a JavaScript array?
To substitute random items in a JavaScript array, you can use Math.random() along with map() to replace selected elements with new values while keeping others in their original positions. For example, if you have an array [m, n, o, p] and want to replace two random items with 'a', the result might be [a, a, o, p] where m and n were randomly selected and replaced. Using Math.random() Method The Math.random() method returns a pseudo-random number between 0 (inclusive) and 1 (exclusive). It's used to generate random indices for array element selection. Syntax Math.random(); ...
Read MoreHow to detect and replace all the array elements in a string using RegExp in JavaScript?
In JavaScript, you can detect and replace all array elements in a string using regular expressions (RegExp). This technique is useful for text processing, content filtering, and dynamic string manipulation where you need to replace multiple values at once. Let's explore different methods to detect and replace all array elements in a string using RegExp in JavaScript. Using RegExp with join() Method The most efficient approach is to create a single regular expression pattern by joining array elements with the OR operator (|). Syntax new RegExp(array.join('|'), 'flags') Example: Basic Replacement ...
Read MoreApply an IF condition to every element in an HTML table with JavaScript?
HTML tables are created using the tag with for table rows and for data cells. JavaScript allows you to apply conditional logic to each table element dynamically. This article demonstrates how to use JavaScript to apply if conditions to every element in an HTML table, enabling dynamic content modification based on specific criteria. Using document.querySelectorAll() with forEach() The document.querySelectorAll() method returns a static NodeList of all elements matching the specified CSS selector. Combined with forEach(), it allows you to iterate through table elements and apply conditions. Syntax document.querySelectorAll(selector).forEach(function(element) { ...
Read MoreMake strings in array become keys in object in a new array in JavaScript?
The task we are going to perform in this article is to make strings in an array become keys in objects within a new array in JavaScript. Let's consider a simple array: let array = ['bike', 'car']; We want to transform this array into a new array where each string becomes a key in an object with an empty array as its value: let newArray = [{bike: []}, {car: []}]; Let's explore different methods to accomplish this transformation in JavaScript. Using map() Method The map() method creates a new array ...
Read MoreRemove element by id in JavaScript?
Removing elements by ID is a fundamental DOM manipulation technique in JavaScript. You can remove elements using getElementById() to select the element and remove() to delete it from the page. JavaScript provides several ways to dynamically modify web page content. Removing elements by their ID is one of the most common operations when creating interactive web applications. Methods for Removing Elements by ID There are two main approaches: Using remove() method (Modern) - Direct element removal Using removeChild() method (Legacy) - Parent-based removal Method 1: Using getElementById() ...
Read MoreHow to count number of occurrences of repeated names in an array - JavaScript?
In JavaScript, counting occurrences of repeated names in an array is a common task when working with data analysis and statistics. This involves iterating through an array of objects and tracking how many times each name appears. Let's consider an array consisting of student details with repeated names: var details = [ { studentName: "John", studentAge: 23 }, { studentName: "David", studentAge: 24 ...
Read MoreJavaScript display the result of a function as HTML?
In JavaScript, displaying the result of a function as HTML allows you to dynamically update webpage content based on calculations or user interactions. This is commonly achieved using DOM manipulation methods. A JavaScript function is a block of code that performs a specific task and can return a value. To display this returned value or result as HTML content, we use DOM methods like innerHTML and getElementById(). Syntax Basic function syntax in JavaScript: function functionName(parameter1, parameter2) { // code to be executed return result; } ...
Read MoreHow to remove all blank objects from an Object in JavaScript?
In JavaScript, removing blank or empty values from an object is a common task when cleaning up data. This article explores different methods to filter out empty strings, null values, or undefined properties from objects. Sample Object with Empty Values Let's work with an object containing empty values that we want to remove: const details = { name: 'John', ...
Read MoreHow can I get H1 innerText in JavaScript without the innerText of its child?
Getting the inner text of an HTML element is a common task when developing web applications. In JavaScript, it can be done using the innerText property of the HTMLElement object. However, this only returns the text within that particular element and not any of its child elements. If you need to get just the H1 tag's innertext without including its child elements' innertext, then there are several ways to do so. This article will discuss some methods for getting an H1 tag's innertext in JavaScript without including its child elements' innertext. The rendered text content of a node, ...
Read More