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 405 of 801
How to implement merge sort in JavaScript?
The Merge Sort algorithm is a divide-and-conquer sorting technique that recursively divides an array into smaller subarrays until each contains a single element, then merges them back in sorted order. The algorithm works by continuously splitting the array in half until it cannot be divided further. Once we have individual elements, we merge them back together in a sorted manner, comparing elements from each subarray and placing them in the correct order. Input-output Scenario Consider an unsorted array that we want to sort using merge sort: Input = [12, 34, 11, 1, 54, 25, 67, ...
Read MoreHow to Split large string in to n-size chunks in JavaScript?
A string is an order of one or more characters that contain numbers, letters, symbols, or special characters. In JavaScript strings are immutable. i.e., once you create a string you cannot change its value. For example, consider the following snippet here we have created a string variable and assigned a value (Tutorialspoint) to it. In the next statement, we are trying to change the contents of the string at the first index. Then we are displaying the contents of the string. let x = 'Tutorialspoint'; x[0] = 't'; console.log(x); // Tutorialspoint ...
Read MoreHow to find maximum value in an array using spread operator in JavaScript?
In this article, we will learn how to find the maximum value in an array using the spread operator with Math.max() in JavaScript. This approach is much cleaner and more efficient than passing array elements individually. What is the Spread Operator? The spread operator (...) expands an array or object into individual elements. It's perfect for passing array elements as separate arguments to functions like Math.max(). // Syntax Math.max(...arrayName) The Problem with Manual Approach Without the spread operator, you'd need to pass each array element individually to Math.max(), which becomes tedious for large ...
Read MoreHow to use spread operator to join two or more arrays in JavaScript?
The spread operator (...) provides a clean, modern way to join multiple arrays in JavaScript. It can be used for both immutable merging (creating new arrays) and mutable merging (modifying existing arrays). What is the Spread Operator? The spread operator (...) expands array elements, allowing you to copy values from one array to another. It performs a shallow copy of the original array. const mergedArray = [...array1, ...array2]; Method 1: Immutable Array Joining This approach creates a new array containing elements from all source arrays, leaving the original arrays unchanged. Joining Two ...
Read MoreIs 'floating-point arithmetic' 100% accurate in JavaScript?
In JavaScript, floating-point arithmetic is not 100% accurate due to how computers store decimal numbers in binary format. This leads to precision errors that can affect calculations involving decimal values. Understanding Floating Point Numbers A floating-point number is any number with a decimal point, such as 1.52, 0.14, or -98.345. In JavaScript, all numbers are stored using the IEEE 754 double precision format, which uses 64 bits to represent numbers. Sign Exponent (11 bits) Mantissa/Fraction (52 bits) ...
Read MoreHow to select a radio button by default in JavaScript?
Radio buttons allow users to select one option from multiple choices. Unlike checkboxes, only one radio button in a group can be selected at a time. To make a radio button selected by default, you can use the HTML checked attribute or JavaScript's checked property. Basic Radio Button Syntax Radio buttons are created using the HTML tag with type="radio": Option 1 Option 2 Method 1: Using HTML checked Attribute The simplest way to select a radio button by default is adding the checked attribute directly in HTML: ...
Read MoreHow to clone an array using spread operator in JavaScript?
In this article, we are going to discuss how to use the spread operator to clone an array in JavaScript. Cloning is the process of copying one array into another array. Previously, the slice() method was used to clone an array, however, ES6 now provides the spread operator (...) to simply clone an array. What is Array Cloning? An Array is a data structure in JavaScript that can hold multiple values at once. Cloning involves copying an array's elements to create a new independent array. ...
Read MoreHow to convert a string to a floating point number in JavaScript?
In JavaScript, converting a string to a floating point number is a common task that can be accomplished through several methods. This article explores three effective approaches to achieve this conversion. Using the parseFloat() Method The parseFloat() function is the most direct way to convert a string to a floating point number. It parses a string and returns the first number found, including decimal values. Syntax parseFloat(string) Note: If the first character cannot be converted to a number, parseFloat() returns NaN. Example: Basic parseFloat() Usage ...
Read MoreHow to remove the 0th indexed element in an array and return the rest of the elements in JavaScript?
Arrays in JavaScript are zero-indexed data structures where elements can be accessed using their index positions. The first element is at index 0, the second at index 1, and so on. const colors = ["Green", "Maroon", "Blue"]; // Index: 0 1 2 This article explores different methods to remove the first element (at index 0) from an array and return the remaining elements. Using the shift() Method The shift() method removes the first element from an array ...
Read MoreHow to find the common elements between two or more arrays in JavaScript?
In JavaScript, finding common elements between two or more arrays is a frequent requirement in web development. Arrays are objects that store multiple values, and there are several efficient methods to identify shared elements across them. Using for Loop The traditional approach uses nested for loops to compare each element of one array with every element of another array. This method works by creating an empty array and pushing matching elements into it. Example 1 Here's how to find common elements between two numeric arrays: Common elements between ...
Read More