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
Object Oriented Programming Articles
Page 101 of 589
How can I disable JavaScript click function if link starts with #?
In JavaScript, you can disable click functionality for links that start with "#" using preventDefault() and CSS selectors. This technique prevents hash links from executing click handlers while allowing normal links to work. Problem Sometimes you need to apply click handlers to most links but exclude hash links (href="#") that are used for navigation or placeholder purposes. Solution Using jQuery Use the :not() selector to exclude links with href="#" from the click handler: Disable Hash Link Clicks ...
Read MoreUnicode Property Escapes JavaScript Regular Expressions
Unicode property escapes in JavaScript regular expressions allow you to match characters based on their Unicode properties using the u flag. This feature enables precise matching of characters by their Unicode categories, scripts, or properties. Syntax /\p{PropertyName}/u /\P{PropertyName}/u // Negated form The \p{} matches characters with the specified property, while \P{} matches characters WITHOUT that property. Common Unicode Properties Property Description Example Characters Letter Any letter A, B, α, β, 中 Number Any number 1, 2, ①, ② Emoji_Presentation Emoji characters 😀, 🌟, 🎉 ...
Read MoreSum is to be calculated for the numbers in between the array's max and min value JavaScript
We need to write a function called sumBetween() that takes an array of two elements and returns the sum of all integers between those values, including both endpoints. For example: [4, 7] = 4+5+6+7 = 22 [10, 6] = 10+9+8+7+6 = 40 Understanding the Problem The function should work regardless of which number is larger. Whether we pass [4, 7] or [7, 4], we need to sum all integers from 4 to 7 inclusive. Solution Using Mathematical Formula We can use the mathematical formula for sum of consecutive integers: sum from 1 ...
Read MoreLookbehind Assertions JavaScript Regular Expressions
Lookbehind assertions in JavaScript regular expressions allow you to match a pattern only when it's preceded by a specific pattern. They use the syntax (? Syntax // Positive lookbehind - match if preceded by pattern (?Example: Extracting Prices with Positive Lookbehind This example matches numbers that are preceded by a dollar sign: Lookbehind Assertions The prices are $50, $99, $121 and $150. But 25 and 75 are not prices. Extract Prices ...
Read MoreGet the item that appears the most times in an array JavaScript
Let's say we are required to write a function that takes in an array of string/number literals and returns the index of the item that appears for the most number of times. We will iterate over the array and prepare a frequency map, and from that map we will return the index that makes the most appearances. Example const arr1 = [12, 5, 6, 76, 23, 12, 34, 5, 23, 34, 65, 34, 22, 67, 34]; const arr2 = [12, 5, 6, 76, 23, 12, 34, 5, 23, 34]; const mostAppearances = (arr) => { ...
Read MoreNamed capture groups JavaScript Regular Expressions
Named capture groups in JavaScript regular expressions allow you to assign names to captured groups, making your code more readable and maintainable. Instead of accessing groups by index, you can reference them by meaningful names. Syntax // Named capture group syntax /(?pattern)/ // Accessing named groups match.groups.name Basic Example Named Capture Groups const text = "The year I graduated was 2012."; ...
Read MoreHow to slice an array with wrapping in JavaScript
Let's say, we are required to write an array method that overwrites the default Array.prototype.slice(). Usually the Array.prototype.slice() method takes in two arguments the start index and the end index, and returns a subarray of the original array from index start to end-1. What we wish to do is make this slice() function so it returns a subarray from index start to end and not end-1. We iterate over the array using a for loop which is faster than many array methods. Then return the required subarray, lastly we overwrite the Array.prototype.slice() with the method we just wrote. ...
Read MoreExplain the callback Promise.finally in JavaScript
The Promise.finally() method executes a callback function when a promise settles, regardless of whether it's fulfilled or rejected. This method is essential for cleanup operations and tasks that must run after promise completion. Syntax promise.finally(callback) Parameters callback: A function that executes when the promise settles. It receives no arguments and its return value is ignored. How It Works Unlike .then() and .catch(), the finally callback doesn't receive the promise's result or rejection reason. It simply indicates that the promise has completed. Example with Fetch API ...
Read MoreHow to reduce an array while merging one of its field as well in JavaScript
Consider, we have the following array of objects − const arr = [{ id: 121, hobby: 'cycling' }, { id: 125, hobby: 'jogging' }, { id: 129, hobby: 'reading' }, { id: 121, hobby: 'writing' }, { id: 121, hobby: 'playing football' }, { id: 125, hobby: 'cooking' }, { id: 129, hobby: 'horse riding' }]; ...
Read MoreExplain 'dotAll' flag for regular expressions in JavaScript
The dotAll flag returns true or false depending upon if the s flag has been set in the regular expression. The s flag enables "dotAll" mode, where the dot . metacharacter matches any character, including newlines. What is the dotAll Flag? By default, the dot . in regular expressions matches any character except newlines. When the s flag is used, the dot can also match newline characters (, \r, etc.), making it truly match "any" character. Syntax regex.dotAll // Returns true if 's' flag is set, false otherwise Example: Checking dotAll Flag ...
Read More