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
Javascript Articles
Page 333 of 534
Pad a string using random numbers to a fixed length using JavaScript
We need to write a function that takes a string and a target length, then pads the string with random numbers until it reaches the specified length. This is useful for creating fixed-length identifiers or codes. How It Works The function uses recursion to add random digits (0-9) to the end of the string until it reaches the target length. Each recursive call generates a new random digit using Math.random(). Implementation const padString = (str, len) => { if(str.length < len){ const random ...
Read MoreHow to display HTML element with JavaScript?
In this tutorial, we will learn how to display an HTML element using JavaScript. HTML elements are the components that make up an HTML format file. These components are in charge of constructing web pages and defining their content. In HTML, an element typically consists of a start tag, content, and a closing tag. Some HTML elements are − Starting Tag Content Ending Tag ...
Read MoreHow to set the color of the left border with JavaScript?
In this tutorial, we will learn how to set the color of the left border with JavaScript. To set the color of the left border in JavaScript, use the borderLeftColor property. Set the color on this property that you want for the border. We could also apply the borderColor property to set the color of the left border. A border is an HTML element's outline. Different sides can be set with different border colors. To set the color of the left border with JavaScript, we have different ways − Using the style.borderLeftColor property ...
Read MoreIs their a JavaScript Equivalent to PHP explode()?
The JavaScript equivalent to PHP's explode() function is split(). Both functions break a string into an array based on a specified delimiter. Syntax string.split(separator, limit) Parameters separator: The character or string to split by (required) limit: Maximum number of splits (optional) Basic Example JavaScript split() Example var str = '087000764008:Rank:info:result'; var arr = str.split(":"); ...
Read MoreOrder items alphabetically apart from certain words JavaScript
In JavaScript, you can sort an array alphabetically while keeping certain priority words at the top. This is useful when you need specific items to appear first, regardless of alphabetical order. Let's create a function excludeSort(arr, ex) where arr is the array to be sorted and ex contains the priority words that should appear at the top. How It Works The sorting logic uses a custom comparator that: Places priority words (from ex array) at the top Sorts remaining words alphabetically Uses Array.includes() to check if a word is in the priority list Example ...
Read MoreHow to create JavaScript data grid for millions of rows?
Displaying millions of rows efficiently requires JavaScript grids that use virtual rendering techniques. These grids only render visible rows in the DOM, dramatically improving performance compared to traditional approaches that render all data at once. Popular Data Grids for Large Datasets S. No Grid Description Max Rows ...
Read MoreSet whether or not an element should be visible while not facing the screen with JavaScript?
Use the JavaScript backfaceVisibility property to control whether an element should be visible when its back face is turned toward the viewer. This property is particularly useful with CSS 3D transforms and animations. Syntax element.style.backfaceVisibility = "visible|hidden|inherit|initial"; Property Values Value Description visible Default. Back face is visible when turned away from user hidden Back face is hidden when turned away from user inherit Inherits value from parent element Example: Interactive Backface Visibility Toggle The following example demonstrates how to toggle the backfaceVisibility ...
Read MoreHow to check existence of NaN keyword in an array JavaScript
We have an array of elements that contains both truthy and falsy values. Our job is to write a function that returns an array with indices of those elements which are NaN in the original array. NaN !== NaN The datatype of NaN is actually number. Although NaN is a falsy value, it has a peculiar property that no other datatype or variable has. It's that the expression NaN === NaN yields false. And it's only in the case of NaN that this is false. So, we can use this behavior to our advantage and pick out ...
Read MoreHow and why does 'z'['toUpperCase']() in JavaScript work?
In JavaScript, the syntax 'z'['toUpperCase']() works because strings are objects, and you can access object methods using both dot notation and bracket notation. This alternative syntax calls the toUpperCase() method on the string 'z', converting it to uppercase. Syntax 'z'['toUpperCase']() // Returns 'Z' This is equivalent to the more common dot notation: 'z'.toUpperCase() // Returns 'Z' Why Does This Work? In JavaScript, there are two ways to access object properties and methods: Dot notation: object.property Bracket notation: object['property'] String literals like ...
Read MoreJavaScript program to merge two objects into a single object and adds the values for same keys
We have to write a function that takes in two objects, merges them into a single object, and adds the values for same keys. This has to be done in linear time and constant space, means using at most only one loop and merging the properties in the pre-existing objects and not creating any new variable. So, let's write the code for this function − Example const obj1 = { value1: 45, value2: 33, value3: 41, value4: 4, ...
Read More