Understanding indexOf() & lastIndexOf() array methods in Javascript | examples

indexOf() The indexof() method returns the first index of the element found in the array. It returns a value of -1 if the element is not found. This method is helpful when you want to check if a given element is present in an array. let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; let numberIndex = numbers.indexOf(5); console.log(numberIndex); // 4 If the element is not present, the method returns -1....

July 25, 2022 · 1 min

What is splice() method in Javascript? with examples

The splice() method in Javascript is used to remove, replace and/or add elements in an array. The splice() method changes the original array. Syntax Array.splice(start, delete, item1, item2…) The first argument start contains the index value from where we want to add or delete elements. The second argument delete contains the count of the number of elements we want to delete. If we specify additional arguments after the delete argument, they are inserted in the array according to the start index....

July 25, 2022 · 3 min

Understanding sort() method in Javascript | with examples

Introduction The sort() method is used to sort the elements of an array and return the sorted array. The way sort() method works is it converts each element to a string and then comparing their sequences of UTF-16 code values. By default, the sort() method sorts in ascending order. let names = ["Peter", "John", "Abdul", "Morris"]; names.sort(); console.log(names); // ['Abdul', 'John', 'Morris', 'Peter'] Next, we will sort numbers. let numbers = [7, 5, 1, 9, 1, 2, 3]; numbers....

July 25, 2022 · 4 min

Using some() method in Javascript | with examples

The some() method in javascript is used to check if at least one element in the array satisfies the condition specified in its callback function. This method returns false if none of the elements satisfies the condition. let numbers = [2, 4, 6, 8, 100]; let check = numbers.some(function (num) { return num === 100; }); console.log(check); // true If there are no elements that meet the condition then it returns false....

July 24, 2022 · 2 min

Using every() method in Javascript | with examples

The every() method in javascript is used to check if every element in the array satisfies the condition specified in its callback function. This method returns true if all the elements satisfy the condition or it returns false immediately if any element does not satisfy the condition. let numbers = [1, 2, 3, 4]; let check = numbers.every(function(num) { return num > 0; }); console.log(check); // true The every method executes the callback function once for every element in the array numbers and checks if the element is greater than 0....

July 24, 2022 · 2 min

Understanding split() in Javascript | with examples

Introduction The split() method splits a string into an array of substrings. It uses a pattern specified in the argument to divide the string into an ordered list of substrings. let str = "Hello world!"; let arr = str.split(" "); console.log(arr); // ['Hello', 'world!'] In the above example, we have split the string str using a space. The method looks for a single space in the string and splits them into substrings....

July 24, 2022 · 2 min

How to use sets in Javascript? with examples

A set is a collection of unique values. A set can have elements of any data type. Create a set object To create a set object we make use of the new Set() constructor. let set = new Set(); To add an element to a set, we make use of the add() method. let set = new Set(); set.add(1); console.log(set); // {1} set.add(2); console.log(set); // {1, 2} set.add(3); console....

July 23, 2022 · 2 min

What are map objects in Javascript? with examples

Introduction The Map objects are collections of key-value pairs. The key in a map is unique and you can have any value like a string, object or primitive value as a key. The map is similar to normal objects, however, in objects, a key must be a string. We cannot have, for example, an integer as a key in an object. Let’s look at an example: let obj = { fName: "Mickey" }; console....

July 23, 2022 · 3 min

How to reverse an array in Javascript? with examples

Javascript provides the method reverse() to reverse the elements in an array such that the last element becomes the first element, the second last element becomes the second element and so on. The reverse() method mutates the array. Let us look at some examples to demonstrate the method. Reverse the elements in an array We can call reverse() method using the dot notation on the array. Below we have an array numbers that contains integers....

July 23, 2022 · 1 min

Understanding unshift() and shift() methods in Javascript | examples

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

July 19, 2022 · 3 min