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 424 of 801
JavaScript - know the value of GET parameters from URL
To extract GET parameters from a URL in JavaScript, you can use the built-in URL and URLSearchParams APIs. These provide a clean way to parse query strings from URLs. Using URL and URLSearchParams The URL constructor creates a URL object, and its searchParams property gives access to query parameters: GET Parameters Example body { ...
Read MoreGroup array by equal values JavaScript
Let's say, we have an array of string/number literals that contains some duplicate values like this: const array = ['day', 'night', 'afternoon', 'night', 'noon', 'night', 'noon', 'day', 'afternoon', 'day', 'night']; We are required to write a function groupSimilar() that takes in this array and returns a new array where all the repeating entries are grouped together in a subarray as the first element and their total count in the original array as the second element. So, for this example, the output should be: [ [ 'day', 3 ], ...
Read MoreJavaScript 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 MoreForm Object from string in JavaScript
We are required to write a function that takes in a string as the first and the only argument and constructs an object with its keys based on the unique characters of the string and value of each key being defaulted to 0. For example − // if the input string is: const str = 'hello world!'; // then the output should be: const obj = {"h": 0, "e": 0, "l": 0, "o": 0, " ": 0, "w": 0, "r": 0, "d": 0, "!": 0}; So, let's write the code for this function − ...
Read MoreJavaScript Let
The JavaScript let keyword, introduced in ES6 (2015), allows us to declare block-scoped variables. Unlike var, variables declared with let are only accessible within the block where they are defined. Block Scope with let Variables declared with let are confined to their block scope and cannot be accessed outside of it. JavaScript Let Example JavaScript Let Block Scope Test Block Scope ...
Read MoreCheck for Power of two in JavaScript
We need to write a function, isPowerOfTwo(), that takes a positive number and returns a boolean based on whether the number is a power of 2. For example: isPowerOfTwo(3) // false (3 is not a power of 2) isPowerOfTwo(32) // true (32 = 2^5) isPowerOfTwo(2048) // true (2048 = 2^11) isPowerOfTwo(256) // true (256 = 2^8) isPowerOfTwo(22) // false (22 is not a power of 2) Method 1: Using Recursive Division This approach recursively divides the number by 2 until it reaches 1 (power of 2) ...
Read MoreJavaScript Location protocol Property
The location.protocol property in JavaScript returns the protocol scheme of the current URL, including the colon (:). This property is useful for determining whether a page is loaded over HTTP, HTTPS, or other protocols. Syntax location.protocol Return Value Returns a string representing the protocol scheme of the URL, including the trailing colon. Common values include: "https:" - Secure HTTP protocol "http:" - Standard HTTP protocol "file:" - Local file protocol "ftp:" - File Transfer Protocol Example ...
Read MoreCheck if three consecutive elements in an array is identical in JavaScript
We are required to write a JavaScript function, say checkThree() that takes in an array and returns true if anywhere in the array there exists three consecutive elements that are identical (i.e., have the same value) otherwise it returns false. Therefore, let's write the code for this function − Example const arr = ["g", "z", "z", "v" ,"b", "b", "b"]; const checkThree = arr => { const prev = { element: null, count: 0 ...
Read MoreJavaScript multiline Property
The JavaScript multiline property is a read-only property of regular expression objects that returns true if the 'm' modifier (multiline flag) has been set, and false otherwise. The 'm' flag changes the behavior of ^ and $ anchors to match at line breaks within the string. Syntax regexp.multiline Return Value Returns a boolean value: true if the 'm' flag is set false if the 'm' flag is not set Example: Checking multiline Property ...
Read MoreFormatting dynamic json array JavaScript
Let's say, we have an array of objects like this – const arr = [ {"name1": "firstString"}, {"name2": "secondString"}, {"name3": "thirdString"}, {"name4": "fourthString"}, {"name5": "fifthString"}, {"name6": "sixthString"}, ]; We are required to write a function that takes one such array of objects and returns an object with all the properties listed in that object. So, let's write the code for this function. It can be done through the Array reduce method – Using Array.reduce() Method ...
Read More