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 on Trending Technologies
Technical articles with clear explanations and examples
How to get only the first n% of an array in JavaScript?
We are required to write a function that takes in an array arr and a number n between 0 and 100 (both inclusive) and returns the n% part of the array. Like if the second argument is 0, we should expect an empty array, complete array if it's 100, half if 50, like that. And if the second argument is not provided it should default to 50. Therefore, the code for this will be − Syntax const byPercent = (arr, n = 50) => { const requiredLength = Math.floor((arr.length * n) / ...
Read MoreJavaScript - Remove first n characters from string
In JavaScript, removing the first n characters from a string is a common operation when processing text data. There are several efficient methods to accomplish this task. What is String Character Removal? Removing the first n characters from a string means creating a new string that excludes the specified number of characters from the beginning. For example Input − const str = "JavaScript"; const n = 4; Output − Script Different Approaches The following are the different approaches to remove the first n characters from a string ...
Read MoreWhat is onkeypress event in JavaScript?
The onkeypress event in JavaScript triggers when a user presses and releases a key while an element has focus. This event is commonly used with input fields, text areas, and other interactive elements to respond to user keyboard input. Syntax element.onkeypress = function(event) { // Handle keypress }; // Or in HTML Basic Example Here's how to use the onkeypress event to detect when a key is pressed in an input field: ...
Read MoreWith JavaScript DOM delete rows in a table?
To delete rows in a table in JavaScript, use the DOM deleteRow() method. This method removes a row from a table by specifying its index position. Syntax table.deleteRow(index) Parameters The index parameter specifies which row to delete (0-based indexing). Use -1 to delete the last row. Example: Delete Rows One by One The following example shows how to delete table rows. Each click removes the first row: function deleteFirstRow() { ...
Read MoreWhat is the drawback of creating true private methods in JavaScript?
Private methods in JavaScript provide encapsulation by hiding internal functionality from external code. While they offer significant benefits like preventing naming conflicts and creating clean interfaces, true private methods come with notable drawbacks that developers should understand. JavaScript supports private methods through closures (using var, let, const) and ES2022 private class fields (using # prefix). Both approaches create truly private methods that cannot be accessed from outside the class. Main Drawbacks of True Private Methods Creating true private methods in JavaScript has two primary drawbacks: No External Access: Private methods cannot be called from outside ...
Read MoreHow to load a JavaScript file Dynamically?
Loading JavaScript files dynamically allows you to add scripts to your webpage at runtime, which is useful for conditional loading, lazy loading, or modular applications. Basic Dynamic Loading Method The most common approach is creating a element and appending it to the document head: Dynamic JavaScript Loading body { font-family: "Segoe UI", Tahoma, ...
Read MoreAwaiting on dynamic imports in JavaScript.
Dynamic imports in JavaScript allow you to load modules asynchronously at runtime using the import() function. This is particularly useful for code splitting and loading modules only when needed. Syntax const module = await import('./modulePath.js'); // or import('./modulePath.js').then(module => { // use module }); How Dynamic Imports Work The import() function returns a Promise that resolves to the module object. You can use await to handle it asynchronously: Dynamic Imports Example ...
Read MoreFind average of each array within an array JavaScript
We are required to write a function getAverage() that accepts an array of arrays of numbers and we are required to return a new array of numbers that contains the average of corresponding subarrays. Let's write the code for this. We will map over the original array, reducing the subarray to their averages like this − Example const arr = [[1, 54, 65, 432, 7, 43, 43, 54], [2, 3], [4, 5, 6, 7]]; const secondArr = [[545, 65, 5, 7], [0, 0, 0, 0], []]; const getAverage = (arr) => { ...
Read MoreJavaScript Submit textbox on pressing ENTER?
In this article, we will learn to Submit a textbox by pressing ENTER in JavaScript. Submitting a textbox form by pressing the ENTER key is a common requirement in web development, enhancing the user experience by making interactions smoother and more intuitive. Why Enable Form Submission on ENTER Key Press? Allowing users to submit a form by pressing the ENTER key is an efficient way to enhance usability and reduce friction in the user interface. Whether you're building a search bar, login form, or any other type of text input, implementing this feature saves time and makes interactions ...
Read MoreHow can I store JavaScript objects in cookies?
This tutorial will teach us to store JavaScript objects into the cookies. The cookies are the information of website visitors stored in text files. There is a particular mechanism to store the cookies of users' browsers. When new visitors visit the website, the server generates the text and sends it to the user. After that, when users allow access to the web page to retrieve the cookies, the server stores all user information in the text files. Cookies store the user's search history or other information for a better experience. For example, Google stores the user's search history to ...
Read More