In JavaScript, there are two main ways that a variable can be "undefined". The first is when you declare a variable without giving it a value. The second is when you try to access a variable that does not exist.Undefined in JavaScript When a variable is declared without a value, it is automatically given the value of "undefined". This can happen if you forget to assign a value to a variable, or if you intentionally do not assign a value (For example, if you are waiting for user input).If you try to access a variable that does not exist, you will ... Read More
JavaScript provides several ways to loop through arrays and objects. The most common way is the for loop, which is used to iterate through the elements of an array or object. However, there are other ways to loop through arrays and objects, such as the forEach() and map() methods.The forEach() MethodThe forEach() method is used to loop through each element of an array or object. The forEach() method takes a callback function as an argument. The callback function is invoked for each element of the array or object.The forEach() method is similar to the for loop, but it does not ... Read More
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
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
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
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
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
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
In asynchronous JavaScript, there are two ways to schedule tasks – microtask queue and callback queue. Both queues are handled differently by the JavaScript engine.Microtask QueueA Microtask queue is a queue of tasks that are executed after the current task. The microtask queue is handled by the JavaScript engine before it moves on to the next task in the callback queue.ExampleHere’s an example of how microtask queue works − Examples console.log('start'); setTimeout(function() { console.log('setTimeout'); }, 0); ... Read More
In JavaScript, there are various ways to remove elements from an array until the passed function returns true. In this tutorial, we are going to look at 3 methods in detail.Using Array.prototype.filter()The Array.prototype.filter() method can be used to remove elements from an array until the passed function returns true. Please refer to the Array filter() method for more details.Example 1The following code shows how to use this method − Examples var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; function ... Read More