Understanding push() and pop() methods in Javascript | examples

What is the push() method in Javascript? The push() method adds one or more elements to the end of the array and returns the length of the array. The push() method will mutate the original array. Let’s look at some examples to demonstrate the use of push. let numbers = [1, 2, 3, 4, 5]; let count = numbers.push(6); console.log(numbers); // [1, 2, 3, 4, 5, 6] console.log(count); // 6 When you push value 6 in the array numbers, this new value will be added at the end of the array....

July 18, 2022 · 3 min

What is fill() method in Javascript? with examples

The fill() method in Javascript is used to change the elements of an array with a specified value. We can specify a start and end index for this method. If the start and end index is not set, it fills all the array elements with the given value. The fill() method overwrites the original array. Let’s look at examples of using the fill() method. ​​​​let numbers = Array(5).fill(0); console.log(numbers); // [0, 0, 0, 0, 0] In the above example, we created an empty array with 5 elements and then chained the fill() method with the value 0....

July 15, 2022 · 2 min

Template literals - String interpolation in Javascript (ES 6) with examples

Template literals are a new feature in ES6 which makes working with strings easy. In this section, we will look at template literals and how we can use them to interpolate strings. We will also look at an easy way of creating multiple line strings. To use the template literals, we wrap the text in backtick (`) instead of the single or double-quotes. The backtick allows us to interpolate variables and expressions using the ${expression} symbol....

July 13, 2022 · 3 min

Spread operator(...) in Javascript with examples

The spread operator allows an expression to be expanded in places where multiple values are expected. This is helpful in places like function calls, array literals or object literals where multiple values are used. The spread operator syntax consists of three dots(…) prefixed to the iterable object. In this section, we will go through some examples to illustrate the spread operators. Spread operator in arrays We will first look at how to use the spread operator in the case of array literals....

July 13, 2022 · 4 min

Understanding reduce() method in Javascript(with examples)

The reduce() method in Javascript is used to execute a function on each element of an array from left to right and reduce the array to a single value. The reduce() method takes two arguments, the first argument is a function and the second argument is the initial value of the accumulator. To understand why we need an accumulator argument, we need to first understand how the function within the reduce() method works....

July 12, 2022 · 3 min

Understanding the forEach method in Javascript | Examples

The forEach() method in Javascript is used to execute the provided function once for every element of the array. What this means is the forEach() loops through an array and runs a function on every element of the array. For example, let us declare an array numbers consisting of integers from 1 to 6. To loop through numbers we will use the forEach() method. We need to pass a function to the forEach() method....

July 11, 2022 · 2 min

Understanding Closure in Javascript | Examples

A function has access to the scope of the parent function it is defined, even after the execution of the parent function is complete. Basically, in Javascript, the inner function has access to the outer function’s variables and arguments even after the outer function’s execution context is finished. This feature of Javascript is called closure and it is an important concept in Javascript to understand. The concept of closure can be confusing to grasp if you are new to Javascript....

July 11, 2022 · 4 min

Immediately Invoked Function Expression(IIFE) with examples

What is Immediately Invoked Function Expression(IIFE)? Immediately Invoked Function Expression(IIFE) is a function that runs as soon as it is defined. Immediately Invoked Function Expression is an important concept in Javascript. In fact, many of the frameworks and libraries built on Javascript use Immediately Invoked Function Expression. In this section, we will look at how to define and use the Immediately Invoked Function Expression. Before we dive into IIFE’s let us first look at how functions are defined in Javascript....

July 10, 2022 · 2 min

Understanding filter() method in Javascript (with examples)

What is the filter() method in Javascript? The filter() method in Javascript is used to create a new array consisting of elements from the original array that passes certain conditions. Basically, filter() method filters out elements in an array that does not meet the criteria specified in the callback function and retains the ones that pass. Let’s say we have an array consisting of integers. We want to select only the even numbers from the array....

July 9, 2022 · 3 min

Method chaining in Javascript with examples

What Is Method Chaining? Method Chaining in Javascript is a design pattern in which we can call methods one after another without assigning the return value of each method to a variable. You can see this pattern being commonly used on strings, arrays and objects in Javascript. For example, we want to convert an email id to lowercase and remove white spaces if any. To do so we can call first call the toLowerCase() on string and later call the trim() method....

July 8, 2022 · 3 min