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 AmitDiwan
Page 404 of 840
JavaScript lastIndex Property
The lastIndex property in JavaScript is used with regular expressions to track the position where the next search will begin. It only works with the global (g) flag and automatically updates after each match. Syntax regexObject.lastIndex How lastIndex Works The lastIndex property starts at 0 and updates to the position after each match when using exec() or test() with the global flag. JavaScript lastIndex Property Finding Multiple Matches with lastIndex let text = "The king bought an expensive ring."; let regex ...
Read MoreJavaScript : Why does % operator work on strings? - (Type Coercion)
Let's say we have a code snippet that produces some surprising results. First, the modulo operator works with strings (unexpectedly). Second, concatenation of two strings produces awkward results. We need to explain why JavaScript behaves this way through type coercion. Problem Code const numStr = '127'; const result = numStr % 5; const firstName = 'Armaan'; const lastName = 'Malik'; const fullName = firstName + + lastName; console.log('modulo result:', result); console.log('full name:', fullName); modulo result: 2 full name: ArmaanNaN What is Type Coercion? Type coercion is JavaScript's automatic conversion of ...
Read MoreHow to check if every property on object is the same recursively in JavaScript?
When working with nested objects in JavaScript, you might need to check if all leaf values (final non-object values) are identical. This requires a recursive approach to traverse through all nested levels and compare the actual values at the end of each branch. For example, in this object: const obj = { a: 1, b: 1, c: { aa: 1 } }; The function should return true because all leaf values equal 1, even though one is ...
Read MoreStrip quotes with JavaScript to convert into JSON object?
When dealing with JSON strings that have escaped quotes or extra quote wrapping, you need to clean them before parsing. This article shows how to strip unwanted quotes and convert the cleaned string into a JavaScript object. The Problem Sometimes JSON data comes with extra quotes or escaped quote characters that prevent direct parsing. Here's a common scenario where a JSON string is wrapped in extra quotes and has doubled internal quotes: var studentDetails = `"{""name"": ""John"", ""subjectName"": ""Introduction To JavaScript""}"`; console.log("Original string:"); console.log(studentDetails); Original string: "{""name"": ""John"", ""subjectName"": ""Introduction To JavaScript""}" ...
Read MoreFind indexes of multiple minimum value in an array in JavaScript
Suppose we have an array of numbers like this − const arr = [1, 2, 3, 4, 1, 7, 8, 9, 1]; Suppose we want to find the index of the smallest element in the array i.e. 1 above. For this, we can simply use − const min = Math.min.apply(Math, arr); const ind = arr.indexOf(min); The above code will successfully set ind to 0, which indeed is correct. But what we want to achieve is that if there are more than one minimum elements in the array, like in the ...
Read MoreWhat are the uses of the JavaScript WITH statement?
The WITH statement is used to specify the default object for the given property and allow us to prevent writing long lengthy object references. It adds the given object to the head of the scope chain. Important: The with statement is deprecated and not recommended in modern JavaScript. It's disabled in strict mode and can cause performance and security issues. Syntax with (object) { // statements } Basic Example Here's how the with statement works with a simple object: ...
Read MoreHow to print star pattern in JavaScript in a very simple manner?
Here is a simple star pattern that we are required to print inside the JavaScript console. Note that it has to be printed inside the console and not in the output or HTML window: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Here's the code for doing so in JavaScript: Example const star = "* "; // where length is no of stars in longest streak const length = 6; for(let i = 1; i
Read MoreParse array to equal intervals in JavaScript
Let's say, we are required to write a function, say parseEqualInterval() that takes in an array of Numbers of strictly two elements as the first argument and a number n as the second argument and it inserts n-1 equidistant entries between the actual two elements of the original array so that it gets divided into n equal intervals. For example: // if the input array is const arr = [12, 48]; // and the interval is 4 //then the output array should be: const output = [12, 21, 30, 39, 48]; This way the array ...
Read MoreLeaders array JavaScript
An element in an array is a leader if it is greater than all elements on its right side. The rightmost element is always a leader since there are no elements to its right. For example, in the array [23, 55, 2, 56, 3, 6, 7, 1]: 56 is a leader (greater than 3, 6, 7, 1) 7 is a leader (greater than 1) 1 is a leader (rightmost element) Input: [23, 55, 2, 56, 3, 6, 7, 1] Output: [56, 7, 1] Using reduceRight() Method The most efficient approach is ...
Read MoreHow to display only the current year in JavaScript?
To display only the current year in JavaScript, use the getFullYear() method with the Date object. This method returns a four-digit year as a number. Syntax new Date().getFullYear() Example: Display Current Year in HTML Current Year Display The Current Year: document.getElementById("currentYear").innerHTML = new Date().getFullYear(); ...
Read More