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 294 of 652
How to set default values when destructuring an object in JavaScript?
The array and object destructuring feature was introduced in the ES6 version of JavaScript. The destructuring of the array and object allows us to store its values in a separate variable. After that, we can use that variable to access the value of the particular property of the object. The main thing we need to focus on while destructuring the array object is the default values. For example, we have added the property3 variable in the destructuring object, but if the object doesn't contain the property3, destructuring sets the undefined value to the property3 variable. Users can follow ...
Read MoreHow to Sort/Order keys in JavaScript objects ?
In JavaScript, objects store key-value pairs, but their keys aren't automatically sorted. While we can't directly use the sort() method on objects like we do with arrays, we can sort the keys and create a new object with the sorted key-value pairs. Below, we will learn various methods to sort object keys in ascending and descending order. Using sort() Method to Order Keys We can use Object.keys() to get all object keys as an array, sort that array, then reconstruct the object with sorted keys. Syntax let allKeys = Object.keys(originalObject); allKeys.sort(); let sortedObj = ...
Read MoreHow to solve JavaScript heap out of memory on prime number?
The 'heap out of memory' error occurs when JavaScript code exceeds the allocated memory limit. This commonly happens with inefficient algorithms that have high time or space complexity, especially when processing large numbers. When finding prime factors of very large numbers, naive algorithms can quickly exhaust available memory. The key is optimizing the algorithm to reduce both time and space complexity. The Problem: Inefficient Prime Factor Algorithm The following example demonstrates how a poorly optimized algorithm causes memory issues when processing large numbers: Visualizing Memory Error with Large Numbers ...
Read MoreHow can a page be forced to load another page in JavaScript?
In JavaScript, you can force a page to load another page using the window.location object. This object provides three main approaches: window.location.href property, window.location.assign() method, and window.location.replace() method. Each handles browser history differently, making them suitable for different use cases. Using window.location.href The window.location.href property contains the current URL and can redirect users to a new page. This is the most commonly used method for page redirection. Syntax window.location.href = "new_url"; You can also use setTimeout() to redirect after a delay: Delayed Redirect Example ...
Read MoreHow to create an array with random values with the help of JavaScript?
In JavaScript, creating arrays filled with random values is a common requirement for simulations, games, and testing. The Math.random() method generates random numbers between 0 (inclusive) and 1 (exclusive), which can be combined with various array methods to create arrays of random values. Using Math.random() with For Loop The most straightforward approach uses a for loop with Math.random() to populate an array: Syntax // Random decimals between 0 and 1 for (let i = 0; i < arrayLength; i++) { randomArr.push(Math.random()); } // Random integers in a range for (let ...
Read MoreHow to set full-screen iframe with height 100% in JavaScript?
Setting an iframe to full-screen with 100% height is a common requirement in web development. This can be achieved using JavaScript's style.height property along with additional CSS properties to create a proper full-screen experience. The style.height property sets or returns the height of an HTML element as a string value. When combined with other style properties like position: fixed and proper positioning, it creates a true full-screen iframe overlay. Syntax element.style.height = "100%"; element.style.width = "100%"; element.style.position = "fixed"; element.style.top = "0"; element.style.left = "0"; element.style.zIndex = "9999"; Parameters ...
Read MoreHow to set location and location.href using JavaScript?
The window.location object in JavaScript provides methods to get and set the current page URL. You can redirect users to different pages using either location or location.href properties. The window.location object can be written without the window prefix. Common location properties include: location.href − Gets or sets the complete URL location.hostname − Gets the domain name location.protocol − Gets the protocol (http: or https:) location.pathname − Gets the path portion of the URL location.assign() − Loads a ...
Read MoreHow to set stroke width of a canvas circle using Fabric.js ?
The stroke and strokeWidth properties are used to set the stroke color and width of a canvas circle in Fabric.js. These properties allow you to customize the border appearance of circle objects. The Fabric.js Circle class provides a rich set of features for creating interactive circles. Unlike basic HTML5 canvas circles, Fabric.js circles are movable, resizable, and highly customizable with properties for stroke, fill, dimensions, and positioning. Syntax fabric.Circle({ radius: number, stroke: string, strokeWidth: number }); Parameters radius − Specifies the radius of the ...
Read MoreHow to simulate a click with JavaScript ?
Simulating a click with JavaScript allows you to programmatically trigger click events on elements without user interaction. This is useful for automation, testing, or creating dynamic user interfaces. What is Click Simulation? Click simulation means programmatically triggering a click event on an HTML element using JavaScript's click() method or dispatchEvent(). This executes the same actions that would occur if a user physically clicked the element. Method 1: Using the click() Method The simplest way to simulate a click is using the built-in click() method: ...
Read MoreHow to simulate target="_blank" in JavaScript ?
The target="_blank" attribute opens links in a new tab or window. In JavaScript, you can simulate this behavior using the window.open() method, which provides more control over how new windows are opened. The window.open() method is a built-in JavaScript function that opens URLs in new browser windows or tabs. It's supported by all modern browsers and offers flexible options for controlling the new window's behavior. Syntax window.open(URL, name, specs, replace) Parameters URL − The URL to open in the new window. If omitted, opens a blank page. ...
Read More