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 by Imran Alam
Page 5 of 6
How to convert a 2D array to a CSV string in JavaScript?
The CSV (Comma Separated Values) file format is a popular way of exchanging data between applications and data stores. The CSV file format is simple and easy to understand, and many applications and programming languages support it. In JavaScript, there are several ways to convert a 2D array into a CSV string. In this tutorial, we'll look at effective methods: using Array.map() with join(), and a manual approach for handling special cases. Using Array.map() with join() (Recommended) The most straightforward approach is to use Array.map() to process each row and join() to combine elements with commas. ...
Read MoreHow to calculate GCD of two or more numbers/arrays in JavaScript?
The greatest common divisor (GCD) of two or more numbers, also known as the greatest common factor (GCF) or highest common factor (HCF), is the largest positive integer that divides a given number without a remainder. In other words, the GCD is the largest number that is a divisor of both numbers. For example, the GCD of 24 and 36 is 12. Understanding the Euclidean Algorithm The Euclidean algorithm is the most efficient method to calculate GCD. It works by repeatedly applying the formula: gcd(a, b) = gcd(b, a % b) until one number becomes zero. ...
Read MoreHow to return HTML or build HTML using JavaScript?
When building web applications, there are often times when you need to dynamically generate HTML on the client-side. This can be done using JavaScript, and there are different ways to go about it. In this article, we'll show you how to return HTML or build HTML using JavaScript. Method 1: Returning HTML from a Function One way to dynamically generate HTML is to return a string of HTML from a function. For example, let's say we have a function that generates a list item: Returning HTML from Functions ...
Read MoreHow to get the first non-null/undefined argument in JavaScript?
In JavaScript, there are often times when we need to find the first non-null/undefined argument in a function. This can be a tricky task, but luckily there are a few methods that can help us accomplish this. Using Array.prototype.find() One method that can be used to get the first non-null/undefined argument in JavaScript is the Array.prototype.find() method. This method returns the value of the first element in an array that passes a given test. In our case, we can use this method to find the first non-null/undefined argument by passing a test that checks if the argument is ...
Read MoreHow to run a given array of promises in series in JavaScript?
In JavaScript, there is a method called "Promise.all" that allows you to run an array of promises in parallel. However, sometimes you may want to run your promises in series instead. This can be useful if you want to make sure that each promise is executed one after the other, or if you need to use the result of one promise in the execution of the next promise. There are a few different ways that you can run an array of promises in series in JavaScript. In this article, we'll take a look at a few of them. ...
Read MoreHow to get the sum of the powers of all numbers in JavaScript?
In this tutorial, we'll be discussing how to get the sum of the powers of all the numbers from start to end in JavaScript. We'll be using the built-in Math.pow() method to calculate the powers and the Array reduce() method to sum up the values. Using the Math.pow() Method The Math.pow() method allows us to calculate the power of a number. We can use this method to calculate the powers of all the numbers from start to end. Syntax Math.pow(base, exponent); Parameters Base − The base number. ...
Read MoreHow to apply function against an accumulator and each key of object in JavaScript?
In JavaScript, we can use the reduce() method to apply a function against an accumulator and each element of an array (from left to right). This method is particularly useful when working with arrays of objects where we need to process each key-value pair. The reduce() method is called on a given array and takes a callback function as its first argument. Please refer to Array reduce() for more details. Syntax array.reduce(callback[, initialValue]) Parameters callback − Function to execute on each value in the array. ...
Read MoreWhat is difference between forEach() and map() method in JavaScript?
JavaScript provides several ways to loop through arrays. Two of the most commonly used methods are forEach() and map(). While both iterate through array elements, they serve different purposes and have distinct characteristics. The forEach() Method The forEach() method executes a callback function for each array element. It's designed for performing side effects like logging, updating DOM elements, or modifying external variables. Importantly, forEach() always returns undefined. Syntax array.forEach(function(element, index, array) { // Execute code for each element }); forEach() Example ...
Read MoreWhat is the difference between undefined and not defined in JavaScript?
In JavaScript, understanding the difference between "undefined" and "not defined" is crucial for debugging and writing clean code. While they may seem similar, they represent fundamentally different states in JavaScript. What is Undefined? undefined is a primitive value in JavaScript that indicates a variable has been declared but not assigned a value. It's also the default return value for functions that don't explicitly return anything. Common Cases of Undefined Variables declared without initialization Object properties that don't exist Array elements that haven't been assigned ...
Read MoreWhat is difference between unescape() and escape() functions in JavaScript?
JavaScript provides two legacy functions for dealing with encoded strings: escape() and unescape(). The escape() function encodes a string, making certain characters safe for URLs, while unescape() decodes an encoded string back to its original form. ⚠️ Important: Both escape() and unescape() are deprecated. Use encodeURIComponent()/decodeURIComponent() or encodeURI()/decodeURI() instead. Syntax escape(string) unescape(encodedString) Key Differences Function Purpose What it does escape() Encodes special characters Converts characters like spaces, punctuation to %XX format unescape() Decodes encoded characters Converts %XX sequences back to original characters ...
Read More