Found 6685 Articles for Javascript

How to apply function against an accumulator and each key of object in JavaScript?

Imran Alam
Updated on 02-Jul-2022 07:41:00

425 Views

In JavaScript, We can use the reduce() method to apply a function against an accumulator and each key of an object (from left to right).The reduce() method is called on a given array and takes in a callback function as its first argument. Please refer to Array reduce() for more details. Let’s look at the syntax of this method.Syntaxarray array.reduce(callback[, initialValue]);Parameterscallback − Function to execute on each value in the array.initialValue − Object to use as the first argument to the first call of the callback.The callback function is passed with four argumentsThe first argument is the accumulator, which is ... Read More

How to get the sum of the powers of all numbers in JavaScript?

Imran Alam
Updated on 02-Jul-2022 07:38:44

388 Views

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() methodThe 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.SyntaxMath.pow(base, exponent) ;ParametersBase − The base number.Exponents − The exponent to which to raise the base.ExampleFor example, if we want to get the sum of ... Read More

How to run a given array of promises in series in JavaScript?

Imran Alam
Updated on 01-Jul-2022 12:44:42

881 Views

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.Promise.prototype.then()One way to run ... Read More

How to get the first non-null/undefined argument in JavaScript?

Imran Alam
Updated on 01-Jul-2022 12:37:05

3K+ Views

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 thisUsing 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 not null/undefined.ExampleIn the example ... Read More

How to return HTML or build HTML using JavaScript?

Imran Alam
Updated on 01-Jul-2022 12:31:15

15K+ Views

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.Returning HTML from a functionOne 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 −function generateListItem(text) {    return '' + text + ''; }We can then use this function to generate HTML −function ... Read More

How to calculate GCD of two or more numbers/arrays in JavaScript?

Imran Alam
Updated on 01-Jul-2022 12:25:50

543 Views

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.How to calculate the GCD of two numbers?There are a few different ways to calculate the GCD of two numbers, but the most common method is the Euclidean algorithm.The Euclidean algorithm is an iterative method that starts with two numbers, ... Read More

How to convert a 2D array to a CSV string in JavaScript?

Imran Alam
Updated on 21-Jul-2022 14:34:55

896 Views

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 a number of ways to convert an array of data into a CSV string. In this tutorial, we'll look at two popular methods: the Array.join() method and the JSON.stringify() method.Using the Array.join() MethodThe Array.join() method is a built-in method of the JavaScript Array object. It can be used to join the elements of an array into a single string. The ... Read More

How to convert a string into an integer without using parseInt() function in JavaScript?

Imran Alam
Updated on 01-Jul-2022 12:06:56

2K+ Views

The parseInt() is a built-in function in JavaScript that parses a string and returns an integer. However, there are times when we want to convert a string into an integer without using this function. In this article, we'll explore how to do just that.Using the unary plus operatorOne way to convert a string into an integer is by using the unary plus operator. This operator converts its operand into a number. For instance, the following program converts the string "123" into the number 123 −Example 1    Examples                   ... Read More

How to return true if the parent element contains the child element in JavaScript?

Imran Alam
Updated on 01-Jul-2022 08:51:52

6K+ Views

In this tutorial, we are going to look at how we can return true if the parent element contains the child element in JavaScript. Assuming you have two HTML elements, a parent element, and a child element, and you want to know if the parent element contains the child element.Using the Node.contains() methodThe Node interface's contains() method returns a Boolean value, indicating whether a node is a descendant of a given node or not.If you want to know if the parent element contains the child element, you can use the Node.contains() method.Here's a simple example −    Some text The ... Read More

How to set the minimum allowed scale value of Rectangle using FabricJS?

Rahul Gurung
Updated on 30-Jun-2022 14:33:09

93 Views

In this tutorial, we are going to learn how to set the minimum allowed scale of Rectangle using FabricJS. Rectangle is one of the various shapes provided by FabricJS. In order to create a rectangle, we will have to create an instance of fabric.Rect class and add it to the canvas.We can customize a rectangle object by adding a fill colour to it, eliminate its borders or even make changes in its dimensions. Similarly, we can also set its minimum allowed scale by using the minScaleLimit property.Syntaxnew fabric.Rect({ minScaleLimit : Number }: Object)ParametersOptions (optional) − This parameter is an Object ... Read More

Advertisements