JavaScript Program To Find Factorial Of A Number

In this tutorial, you’ll learn how to write a program to find factorial of a number. This post is a part of the JavaScript coding practice series. Factorial Of A Number First, let’s try to understand what the factorial means before trying to implement it. The factorial of a number n means, the product of all positive integers less than or equal to n. For example, the factorial of 4 is 4*3*2*1 = 24. [Read More]

How To Check If Object Has Key In JavaScript

In this tutorial, you’ll learn how to check if an object has key in JavaScript. You’ll be making use of in operator for checking if a key is present or not.

let obj = {
    'name' : 'Roy'
}

if('name' in obj){
    console.log('Found')
} else {
    console.log('Not found')
}

How To Check If Object Is Array In JavaScript

In this tutorial, you’ll learn how to check if an Object is an array in JavaScript. For checking if an array, JavaScript provides a built in method called isArray.

let arr = [];
    
if(Array.isArray(arr)){
    console.log('It is an array!!')
} else {
    console.log('Not an array!!')
}

How To Handle Promise Inside Loop

In this tutorial, you’ll learn how to handle JavaScript promise inside loop. Promises are an important concept in JavaScript programming. Getting a basic understanding about how Promises work in JavaScript is a must for every JavaScript developer. Assuming you have an understanding about how promises work, you will be able to understand this tutorial. Promise Inside For/ForEach Loop Create a folder called promise_loop. Initialize the project folder using npm. mkdir promise_loopcd promise_loopnpm initCreate a file called helper. [Read More]

How To Handle Promise Inside Promise

In this tutorial, you’ll learn how to handle JavaScript promise inside promise. Sometimes it’s required that the result from a promise is the input to another promise and so on. So, let’s see how a promise inside promise or promise chaining can be implemented. Using Promise Inside Promise Let’s start by creating a project directory called promise_inside. Navigate to the project directory and initialize the project using npm. mkdir promise_insidecd promise_insidenpm initOnce the project has been initialized, install the node module request. [Read More]

JavaScript : Find Duplicate Values In An Array

In this quick tutorial, you’ll learn how to find duplicates in an array using JavaScript. For finding duplicate values in JavaScript array, you’ll make use of the traditional for loops and Array reduce method. Using For Loop Let’s see how you can find duplicates in an array using for loop. The logic is you’ll separate the array into two array, duplicate array and unique array. You’ll iterate over the values in the array and push the items into unique list if it already doesn’t exist. [Read More]

How To Access Previous Promise Result In Then Chain

In this tutorial, you’ll learn how to access promise result in .then promise chain. In nested JavaScript Promise, sometimes it’s required to access one promise result in the promise chain. In such cases, you tend to assign promise result to a variable and then access it in promise chain. Let’s have a look at how you can access promise results in the entire chain. function getPromise(i){return new Promise(function(resolve, reject){setTimeout(function(){resolve({"data" : i});}, 2000);})}function queryPromiseChain(){let promiseOneResult;let promiseTwoResult;getPromise(1). [Read More]

How to Make API Calls Inside For-loop In JavaScript/

Making API call inside for loop is a common scenario that we face while creating web applications. In this tutorial, you’ll learn how to make API calls inside for loop in JavaScript. You’ll focus on how to make the API calls asynchronous inside for loop in JavaScript using Promise. Getting Started Let’s assume that you have an array of JSON objects with Ids. [{"Id": 10},{"Id": 20},{"Id": 30},{"Id": 40},{"Id": 50}]You need to iterate through the JSON objects array and for each Id you need to make an API call. [Read More]

JavaScript Array Includes Method

In this tutorial, you’ll learn about the JavaScript Array includes method. This method is used to check if an array contains or includes a particular value. Let’s have a look at an example: let arr = [1,2,3,4,5]; if(arr.includes(3)) { console.log('item found') } else { console.log('item not found') } includes method also accepts an optional parameter to specify the index from which to search. By default, the index to search parameter is zero. [Read More]