Found 6685 Articles for Javascript

How to convert a string of any base to an integer in JavaScript?

Mayank Agarwal
Updated on 22-Apr-2022 14:05:55

405 Views

An integer can be represented in various formats as described in the computed languages i.e. Binary, Decimal, Hexadecimal, etc. A Binary number consists of two digits only i.e. 0 & 1, whereas a decimal consists of digits from 0 to 9.We can convert a string to an integer by using the parseInt() conversion by default. But in other cases when the integer is represented in some other form we also need to pass the base string to decode it into the resultant number.Below is the syntax of parsing a string to an int.Syntax1. When the base value is providedparseInt(string_value, base)2. ... Read More

JavaScript: Converting a CSV string file to a 2D array of objects

Mayank Agarwal
Updated on 22-Apr-2022 13:59:44

603 Views

A CSV is a comma-separated value that is stored in a file with the .csv extension. This allows the user to store data in a tabular or spreadsheet format.In this article, we will be exploring how to convert the data of a CSV string to 2D array objects where the first row of the string defines the file headers. We will be first reading this row and then mapping the remaining values with them.We will be using the following function that will act as a prototype function from array and string since it will be helpful in mapping the data ... Read More

JavaScript: Computing the average of an array after mapping each element to a value

Mayank Agarwal
Updated on 22-Apr-2022 13:52:15

763 Views

In this article, we are going to fetch all the values from an array and after mapping each element to a numerical value we will calculate its sum thus to compute its average.Given below is an array consisting of values. We will compute the average by summing up the values and then dividing it by the number of values available.Input[1, 2, 3, 4, 5]Output3Explanation(1+2+3+4+5)/5 = 3Input[2, 4, 7, 9, 1, 3, 8]Output4.85Explanation(2+4+7+9+1+3+8)/7 = 4.85Approach #1We will iterate over the list of elements using the forEach() loop.On iterating each element, we will convert it to a value and add it to ... Read More

How to compile a Typescript file?

Mayank Agarwal
Updated on 22-Apr-2022 13:44:46

1K+ Views

In this article are going to explore TypeScript and how to execute the same. TypeScript is an open-source programming language developed and maintained by Microsoft.Typescript is a bit syntactically different from the native JavaScript but also adds additional features to it. Typescript is the superset of JavaScript with a strong pace of development and object-oriented programming. It also does not run directly on the web browsers as JavaScript runsTypescript files need to be combined with JavaScript first and then they work as normal JavaScript. In order to compile and execute the typescript file, we need to install the node and ... Read More

JavaScript: How to check whether an array includes a particular value or not?

Mayank Agarwal
Updated on 22-Apr-2022 13:31:28

168 Views

In this article, we will be discussing the construction of an array. Once the array is created we will be looking out for a value by checking whether it exists in the array or not.But before checking for any particular value, let’s see how the arrays are created in JavaScript.Syntaxlet array = [arrayItem1, arrayItem2, arrayItem3 ...]Once the array is created, let’s see how to search for a specific value from the array. There are multiple approaches to searching for whether an element exists in the array or not. Below we are going to discuss the same.Approach #1This is a traditional ... Read More

JavaScript: How to check if a number is NaN or finite?

Mayank Agarwal
Updated on 22-Apr-2022 13:24:59

412 Views

Often we need to check whether a finite number or undefined returns the same to the user. In this article also we will be checking if a number is actually a finite number or Not a Number, i.e., NaN.Below is the method to check whether a number is finite or not.isNaN()This is a method provided by JavaScript to check whether a number is a finite number or not. If this method returns false, then the number is a finite number else the number is not finite.SyntaxisNaN(number )Example 1In the below example, we are going to use the above method to ... Read More

JavaScript: How to check if a number is even without using the modulo operator?

Mayank Agarwal
Updated on 22-Apr-2022 13:20:34

713 Views

Often we need to check whether a number is odd or even and return the same to the user. In this article also we will be checking whether a number is even or not but without using the modulo operator. It’s pretty easy to check whether a number is even by using the modulo operator. But we can also check a number with other possible methods.Below we are going to see the other methods for checking a number and its properties.Approach #1: Using the for() loopIn this method, we are going to use the for() loop to check whether a ... Read More

How to chain asynchronous functions in JavaScript?

Mayank Agarwal
Updated on 22-Apr-2022 12:54:13

1K+ Views

JavaScriptis a single-threaded synchronous function that performs operations. It is a timeconsuming operation that blocks other operations of the thread.We can use the asynchronous programming provided by JavaScript that performs functions without blocking other operations of the thread. This can be done by asynchronous code like promises or async functions (which basically are cleaner promises).Asynchronous functions are cool but the time of their execution is not certain which might create a problem. Also, it is not easier to track async functions for any potential errors.In this article, we will be exploring the chaining of asynchronous functions using ES2015+ features like ... Read More

How to call the constructor of a parent class in JavaScript?

Mayank Agarwal
Updated on 22-Apr-2022 12:46:27

2K+ Views

In this article, we are going to explore constructors in an object. We can create inheritance in terms of object, i.e. a parent object can have one or multiple child objects. Now we can call the constructors of a parent object from the child object.ConstructorThese are the instances of a class that is commonly referred to as an Object. The new keyword from JavaScript uses a constructor to be called when an object needs to be declared or created. We can set properties into an object using these constructors.Inheritance in JavaScriptThis is the ability of an object to access the ... Read More

How to call a JavaScript function in HTML?

Mayank Agarwal
Updated on 22-Apr-2022 12:40:47

2K+ Views

In this article, we will be exploring the calling and initialization of the JavaScript function from an HTML template. We require the JavaScript function to execute the desired methods over the input passed.In this tutorial, we will be discussing two major approaches to calling a JavaScript function from an HTML page.In the first Approach, we are going to take a simple input tag and a Submit button associated with it. Once the button is clicked we will see a dialog box that pops up on the screen as an alert. This button on click calls invokes the JavaScript function to ... Read More

Advertisements