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
Front End Technology Articles
Page 384 of 652
How to get the protocol and page path of the current web page in JavaScript?
In JavaScript, you can retrieve both the protocol (like https:// or http://) and the page path of the current webpage using the window.location object and URL constructor. These properties are essential for web development tasks like URL manipulation and navigation. Getting the Protocol The protocol specifies the data transfer method used by the URL. Use window.location.protocol or URL.protocol to retrieve it. Syntax protocol = window.location.protocol; // or protocol = new URL(url).protocol; Using window.location.protocol Protocol Example ...
Read MoreReplacing all special characters with their ASCII value in a string - JavaScript
We are required to write a JavaScript function that takes in a string that might contain some special characters. The function should return a new string with all special characters replaced with their corresponding ASCII value. Understanding Special Characters Special characters are symbols that are not letters, numbers, or spaces. Examples include !, @, #, $, etc. Each character has a unique ASCII code that can be retrieved using the charCodeAt() method. Example Following is the code: const str = 'Th!s !s @ str!ng th@t cont@!ns some special characters!!'; const specialToASCII = str ...
Read MoreHow to return an array whose elements are the enumerable property values of an object in JavaScript?
We can use various methods to get values and keys from an object, but those methods will not return the values as an array, which is very useful in many cases. JavaScript provides the Object.values() method to get an array whose elements are the enumerable property values of an object. Syntax Object.values(obj); This method takes an object as an argument and returns an array whose elements are the property values of the object. Parameters obj: The object whose enumerable property values are to be returned. Return Value An array containing the ...
Read MoreInserting element at falsy index in an array - JavaScript
We are required to write an Array function, let's say, pushAtFalsy() The function should take in an array and an element. It should insert the element at the first falsy index it finds in the array. If there are no empty spaces, the element should be inserted at the last of the array. We will first search for the index of empty position and then replace the value there with the value we are provided with. Understanding Falsy Values In JavaScript, falsy values include: null, undefined, false, 0, "" (empty string), and NaN. However, since 0 ...
Read MoreHow to get the pixel depth and color depth of a screen in JavaScript?
The JavaScript window object provides methods to get screen display information through the screen object. Two important properties are screen.colorDepth and screen.pixelDepth which return the color depth and pixel depth of the browser screen respectively. Color Depth The screen.colorDepth property returns the number of bits used to display one color on the screen. Modern computers typically use 24-bit or 32-bit hardware for color resolution, where: 24-bit color: Supports 16.7 million colors (True Color) 32-bit color: Includes an alpha channel for transparency Syntax screen.colorDepth Example ...
Read MoreArray of adjacent element's average - JavaScript
Let's say, we have an array of numbers: const arr = [3, 5, 7, 8, 3, 5, 7, 4, 2, 8, 4, 2, 1]; We are required to write a function that returns an array with the average of the corresponding element and its predecessor. For the first element, as there are no predecessors, so that very element should be returned. Let's write the code for this function, we will use the Array.prototype.map() function to solve this problem: Example const arr = [3, 5, 7, 8, 3, 5, 7, 4, 2, 8, ...
Read MoreHow to find the inner height and inner width of a browser window in JavaScript?
In this article we are going to learn how to find the inner height and inner width of a browser window using JavaScript. JavaScript defines window object methods and properties. The properties include inner height and inner width, which helps us to calculate the height and width of a window's content area excluding scrollbars, toolbars, and other browser UI elements. There are two main approaches to calculate the inner height and inner width of a window: Using innerHeight and innerWidth properties − The window.innerHeight and window.innerWidth properties return the viewport dimensions including padding ...
Read MoreWhat is the importance of '/g' flag in JavaScript?
The /g flag in JavaScript regular expressions stands for "global" and is essential for finding all matches in a string rather than stopping after the first match. Without this flag, regex methods will only find the first occurrence of a pattern. Understanding Regular Expressions Regular expressions are patterns used to match character combinations in strings. They can be created in two ways: Literal notation: Uses forward slashes with optional flags after the second slash (e.g., /pattern/g). This is preferred when the pattern remains constant. Constructor function: Uses new RegExp("pattern", "flags"). This is useful when the pattern ...
Read MoreBuild maximum array based on a 2-D array - JavaScript
Let's say, we have an array of arrays of Numbers like below − const arr = [ [1, 16, 34, 48], [6, 66, 2, 98], [43, 8, 65, 43], [32, 98, 76, 83], [65, 89, 32, 4], ]; We are required to write a function that maps over this array of arrays and returns an array that contains the maximum (greatest) element from each subarray. So, for the above array, the output should be − [48, 98, 65, 98, 89] Using ...
Read MoreWhat is the importance of '/i' flag in JavaScript?
The /i flag in JavaScript is a regular expression modifier that enables case-insensitive pattern matching. By default, regular expressions are case-sensitive, meaning they distinguish between uppercase and lowercase letters. Understanding Regular Expressions Regular expressions are patterns used to match character combinations in strings. They can be created using two methods: Literal notation: Uses slashes to enclose the pattern (/pattern/flags). Best for static patterns that won't change during execution. Constructor method: Uses the RegExp constructor (new RegExp("pattern", "flags")). Ideal when the pattern needs to be dynamic or constructed at runtime. ...
Read More