JavaScript Array at() Method

JavaScript Array at() method “returns an element at that index, allowing for positive and negative integers”. Negative integers count back from the last element in the array. Syntax array.at(index) Parameters index: It is an index (position) of the array element to be returned. Return value The at() method returns an indexed element from an array. … Read more

JavaScript Array entries() Method

JavaScript Array entries() method “returns a new array iterator object that contains the key/value pairs for each index in the array.” Syntax array.entries() Parameters None. Return value It returns a new iterable iterator object. Example 1: How to Use Array.entries() Method const arr = [“A”, “B”, “C”, “D”]; for (const [index, element] of arr.entries()) { console.log(index, … Read more

JavaScript Array group() Method

JavaScript Array group() method is “used to group the elements of the calling array according to the string values returned by a provided testing function”. Syntax group(callbackFn, thisArg) Parameters callbackFn: It is a function to execute for each element in the array.  thisArg: It is a value to use as this when executing callbackFn. Return value It returns … Read more

How to Reduce an Array of Objects in JavaScript

To reduce an array of objects in JavaScript, you can use the “array.reduce()” method. The array.reduce() method is “used to reduce an array of objects to a single value by executing a callback function for each array element”. Syntax array.reduce(function(total, currentValue, currentIndex, arr), initialValue) Parameters function: It is a callback function. total: It is the … Read more

How to Fix TypeError: Cannot read property ‘toLowerCase’ of undefined

To fix the TypeError: cannot read property of ‘toLowerCase’ of undefined error, you need to “check if the property exists before calling the toLowerCase() function using the ternary operator.” TypeError: cannot read property of ‘toLowerCase’ of undefined error occurs in JavaScript when “you try to call the toLowerCase() method on an undefined value.” This method … Read more

How to Replace Multiple Strings in JavaScript

To replace multiple strings in JavaScript, you can use the “string.replace()”, “string.replaceAll()”, or “split()” and “join()” methods. Method 1: Using the string.replace() function You can use the string.replace() method to replace multiple strings with other strings in JavaScript. let str = “Netflix has StrangerThings, BlackMirror, and HouseOfCards”; let mapObj = { StrangerThings:”Millie Bobby Brown”, BlackMirror:”Anthony … Read more

How to Check If an Array Includes an Object in JavaScript

Here are the ways to check if an array includes an object in JavaScript: Using the “array.includes()” function Using the “array.some()” function Using the “array.filter()” function Using the “array.findIndex()” function Using the “array.find()” function Method 1: Using the array.includes() function You can use the “array.includes()” method to check if an array contains an object. If … Read more

How to Fix TypeError: minicssextractplugin is not a constructor

TypeError: minicssextractplugin is not a constructor error occurs in JavaScript when an incorrect version of webpack or mini-CSS-extract-plugin is installed. To fix the error, install the latest version of the “mini-css-extract-plugin” module. You can install the latest version of “mini-css-extract-plugin” using the below command.  npm i mini-css-extract-plugin –save At the time of this article, the latest version is … Read more

How to Fix TypeError: Reduce of empty array with no initial value

To fix the TypeError: Reduce of empty array with no initial value error, “provide an initial value as the second argument” to the array.reduce() method. The TypeError: Reduce of empty array with no initial value error occurs in JavaScript when you call the reduce() function on an empty array without providing an initial value. Message TypeError: … Read more

How to Fix subquerybuilder.getparameters is not a function in JavaScript

The subquerybuilder.getparameters is not a function error occurs in JavaScript when “you are using a database querying library in JavaScript (like TypeORM or Sequelize) and the method getParameters is being called on an object which does not have such a method”. Reasons The object is not being properly instantiated. The method is not defined within … Read more

How to Fix Fatal JavaScript invalid size error

To fix the Fatal JavaScript invalid size error, ensure you “only access elements within the array’s bounds”. Alternatively, you can restructure your code to use a data structure that can handle the size of the data you are working with. Fatal JavaScript invalid size error occurs when you exceed a limit on the size of some data … Read more

8 Ways to Check If an Array Contains an Element in JavaScript

Here are the ways to check if an array contains an element in JavaScript: Using “array.includes()” method Using “array.indexOf()” method Using “array.some()” method Using the “array.find()” method Using the “array.findIndex()” method Using the “array.filter()” method Using the “array.reduce()” method Using a “for loop” Method 1: Using the Array.includes() The easiest way to check if an … Read more

JavaScript Sleep: How to Pause Code Execution

JavaScript does not have a built-in “sleep()” function that pauses the code execution for a specified period of time. But you can use methods such as setTimeout(), async/await, promises, or generators. How to create a sleep function in JavaScript? To create a sleep function in JavaScript, use the “combination of async/await with the setTimeout() function” … Read more

How to Find the Sum of an Array of Numbers

Here are the ways to find the sum of an array of numbers in JavaScript: Using “array.reduce()” method Using the “array.forEach()” method Using the “array.map()” method Using “for loop” Using “while loop” Method 1: Using array.reduce() method The easiest way to find a sum of an array in JavaScript is to use the “array.reduce()” method. The … Read more

TypeScript Map (With Examples)

TypeScript Map is a new data structure that allows us to store data in the key-value pair and remembers the original insertion order of the keys. It is a new feature of ECMAScript 6 (ES6) that provides advantages over plain JavaScript objects. How to create a Map in TypeScript You can create a Map in TypeScript using this let … Read more

How to Get Current Timestamp in JavaScript

To get a current timestamp in JavaScript, you can use the “Date.now()” function. The Date.now() static method is “used to the number of milliseconds elapsed since the epoch, defined as the midnight at the beginning of January 1, 1970, UTC”. Example console.log(Date.now()); Output Using JavaScript getTime() method We can get the same value using the … Read more

JavaScript Math max() Method

JavaScript Math.max() method is a static method that “returns the number with the highest value“. Syntax Math.max(x, y, z, …) Parameters x, y, z,…: The numbers out of which the maximum is to be determined. Return Value The maximum of all the parameters passed. If no argument is passed, this method returns negative infinity. If … Read more

JavaScript String charCodeAt() Method

JavaScript String charCodeAt() method “returns the Unicode value of the character at the specified index in the string”. The index of the last character is string length – 1. Syntax string.charCodeAt(index) Parameters index: A number representing the index of the character you want to return. Return value The charCodeAt method returns “NaN” if there is no character … Read more

JavaScript Object is() Method

JavaScript Object.is() method is “used to check if two values are the same”. Two values are the same if one of the following holds: Both are undefined Both are null Both are true, or both are false Both strings are of the same length with the same characters in the same order Both are the … Read more

JavaScript Object defineProperty() Method

JavaScript Object defineProperty() is a “static method used to define a new property on an object or modify an existing one and returns the object”. Syntax Object.defineProperty(obj, prop, descriptor) Parameters obj: The object on which to define the property. prop: The name or Symbol of the property to be defined or modified. descriptor: The descriptor for … Read more

5 Easy Ways to Flat an Array in JavaScript

Here are the ways to flat an array in JavaScript. Using Array.prototype.flat() method Using Array.prototype.reduce() method Using Array.prototype.concat() method Using a spread operator Using a recursive function Method 1: Use the Array.prototype.flat() method The best and most reliable way to flatten an array in JavaScript is to use the Array.prototype.flat() method. The Array flat() is … Read more

JavaScript String trimStart() and String trimEnd() Methods

JavaScript String trimStart() JavaScript String trimStart() method is “used to remove whitespace from the beginning of a string”. The following characters are the whitespace characters in JavaScript: A space character A tab character A carriage return character A new line character A vertical tab character A form feed character Syntax string.trimStart() Parameters None Return value … Read more

What is Strict Mode and How to Use It in JavaScript

Strict mode is a feature in JavaScript that enables stricter parsing and error handling of your code. By opting into the strict mode, you can catch common coding mistakes and “unsafe” actions, such as using undeclared variables, accidentally overwriting global objects, or using deprecated language features. To enable strict mode, you add the following directive … Read more

JavaScript encodeURI() Method

JavaScript encodeURI() is “used to encode complete URI”. It encodes the special characters except for (, / ?: @ & = + $ #).  Syntax encodeURI(uri) Parameters uri: It is a string to be encoded as a URI. Return value It returns a new string representing the provided string encoded as a URI. Example 1: … Read more

JavaScript Number isFinite() Method

JavaScript Number.isFinite() is a “static method used to check if a number is a finite number”. It returns true if the number is finite; otherwise false. To check a finite value in JavaScript, use the Number.isFinite() method. Syntax Number.isFinite(value) Parameters value: It is the value to be tested. Return value It returns a boolean value … Read more

JavaScript Array copyWithin() Method

JavaScript Array copyWithin() method “shallow copies part of an array to another location in the same array and returns it without modifying its length”. It does not add elements to the array. Syntax array.copyWithin(target, start, end) Parameters target: It is the index position to copy the elements. start: It is the index position to start … Read more

JavaScript Math abs() Method

JavaScript math.abs() is “used to return the absolute value of a number”. For example, the absolute value of -5 is 5, and the absolute value of 5 is 5. Syntax Math.abs(num) Parameters num: It is a number whose absolute value is to be determined. Return Value If the argument is not negative, it returns the … Read more

JavaScript String StartsWith() Method

JavaScript String startsWith() method is “used to check whether a string begins with the characters of a specified string, returning true or false as appropriate”. In summary, The string startsWith() method in JavaScript “returns true if a string starts with a specified string, false otherwise”. Syntax str.startsWith(searchString[, position]) Parameters searchString: The characters to be searched … Read more

JavaScript Function call() Method

JavaScript Function call() method is “used to call with a given this value and arguments provided individually”. The keyword “this” refers to the “owner” of the function or the object it belongs to. Syntax function.call(this, arg1, arg2, …) Parameters this: It is optional, and it is the value of this provided for a call to a … Read more

JavaScript JSON.stringify() Method

JavaScript JSON.stringify() is a “static method to convert a JavaScript value to a JSON string.” To convert a JSON to a String in JavaScript, you can use the JSON.stringify() method. Syntax JSON.stringify(value, replacer, space) Parameters It accepts three parameters. value: It is the value to be converted into the JSON string. replacer: It is the optional … Read more

JavaScript Array flatMap() Method

JavaScript array flatMap() method is “used to map all array elements and creates a new flat array”. It creates a new array by calling a function for every array element. Syntax let new_array = arr.flatMap(function callback(currentValue[, index[, array]]) { // return element for new_array }[, thisArg]) Parameters callback() function: It is a parameter with the following … Read more

JavaScript String valueOf() Method

JavaScript String valueOf() method “returns the primitive value of the String object”. Syntax string.valueOf() Parameters None. The function does not take any parameters. Return value It is a primitive value of the string. Example: How to Use the String.valueOf() function let data = “Krunal Lathiya”; let res = data.valueOf(); console.log(res); Output The valueOf() method of String returns a primitive … Read more

How to Use JavaScript Exponentiation

The exponentiation (**) operator in JavaScript is “used to return the result of raising the first operand to the power of the second operand”. The ** operator is right-associative, meaning that the expression is evaluated from the right side of the ** to the left side. Syntax x ** y Example 1: Use of Exponentiation … Read more

How to Use ES6 Import in Node.js

To use ES6 import syntax in Node.js, you must enable ES6 modules to support, and there are two ways to do that: Use the file extension .mjs for your module files and run Node.js with the –experimental-modules flag. Add “type”: “module” to your package.json file and use the .js extension for your module files. Method … Read more

JavaScript Error Object

JavaScript Error constructor is “used to create Error objects”. To create a new Error object, use the “new Error()” constructor expression. The instance of an error object is thrown when runtime errors occur. Syntax new Error([message[, fileName[, lineNumber]]]) Parameters message: Optional. A human-readable description of the error. fileName: Optional. The value for the file property on … Read more

JavaScript Math random() Method

JavaScript Math random() function is “used to generate a random number between 0 (inclusive) and 1 (exclusive)”. It accepts no argument and returns the value between 0 to 1, in which 1 is exclusive and 0 is inclusive. Syntax Math.random() Parameters It does not accept any argument. Return Value The Math.random() function returns the floating … Read more

JavaScript ArrayBuffer() Constructor

JavaScript ArrayBuffer object is “used to represent a generic raw binary data buffer.” You cannot modify the contents of an ArrayBuffer directly; instead, you create one of the typed array objects or a DataView object that represents the buffer in a specific format and use that to read and write the contents of the buffer. … Read more

JavaScript encodeURIComponent() Method

JavaScript encodeURIComponent() function is “used to encode a Uniform Resource Identifier (URI) component replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character “. The encodeURIComponent() function encodes “special characters including: , / ? : @ & = + $ #”. Syntax encodeURIComponent(uri) Parameters … Read more

JavaScript Number isNaN: How to Check If Number in NaN

To check if the number is NaN(Not a Number) in JavaScript, you can use the “Number.isNaN()” method. The Number isNaN() method in JavaScript is “used to check if a number is NaN”. If the input value is NaN, it returns true otherwise false. Syntax Number.isNaN(value) Parameters The value parameter is required, which is the value that needs … Read more

JavaScript String split() Method

JavaScript String split() method is “used to split a string into an array of substrings using a specified separator provided in the argument“. It method does not change the original string. Syntax string.split(separator, limit) Parameters Parameter Description separator Optional. A string or regular expression to use for splitting. If omitted, an array with the original … Read more

JavaScript Rest Parameter

JavaScript rest parameter denoted by three dots (…) is “used to represent an indefinite number of arguments as an array”. With the help of a rest parameter, a function can be called with any number of arguments, no matter how it was defined. Syntax function f(x, y, …args) { // … } Parameters One thing to … Read more

JavaScript String includes: Check If String Contains Substring

JavaScript String includes() method is “used to check if a string contains a substring”. If the substring is found, the method returns true. Otherwise, it returns false. Syntax str.includes(searchString, position) Parameters searchString:  It has only one required parameter, which is searchString. It is the substring, which we need to search in the String. position: The position … Read more

How to Convert String to Number in JavaScript

Here are the following ways to convert string to number in JavaScript. Using the “Number()” function Using the “parseInt()” function Using the “parseFloat()” function Using the “unary operator(+)” Using the “Math.floor()” function Using the “Double tilde (~~)” Operator Using the “Multiplication” Method 1: Using the Number() function The easiest way to convert a string to … Read more

How to Redirect to New URL in JavaScript

To redirect web browsers to a new URL from the current page to a new one in JavaScript, you use the “location.href” object. Syntax location.href = “newURL”; Example location.href = “https://appdividend.com/”; Assigning a value to the href property of the location object has the same effect as calling the assign() method of the location object: … Read more

JavaScript Switch Statement

JavaScript switch statement is “used to perform different actions based on different conditions”. You can use the “switch statement” to select one of many code blocks to be executed. You can use multiple if…else…if statements, but it is a bit cumbersome; instead, you can use the switch statement to execute the code based on conditions. … Read more

JavaScript Array keys() Method

JavaScript array keys() method is “used to get a new array iterator object that contains the keys for each index in the array”. Syntax arr.keys() Parameters None. Return value It is a new iterable iterator object. Example 1: How to Use Array keys() Method let arr = [‘a’, ‘b’, ‘c’]; let iteratorObj = arr.keys(); console.log(iteratorObj); So, it … Read more

JavaScript String charAt() Method

JavaScript String charAt() function “returns the character at a specified index within a string”. If the index is out of range (i.e., greater than or equal to the string’s length), the method returns an empty string. Syntax string.charAt(index) Parameters index: It is the required parameter, an integer representing the index of the character you want … Read more

JavaScript Blob

JavaScript Blob is a “group of bytes that holds the data in a file”. A blob has its size and MIME just like that of a simple file. The blob data is stored in the memory or filesystem of a user depending on the browser features and size of the blob. The browser has additional high-level … Read more

JavaScript Array push() Method

JavaScript array push() function is “used to add a new element at the end of an array and returns the new length”. Syntax array.push(element) You can use a push() method to append more than one value to an array in a single call. array.push(item1, item2, item3,…,itemN) Parameters item1, item2: It takes item1, item2 to add … Read more

How to Empty an Array in JavaScript

There are the following ways to empty or clear an array in JavaScript Method 1: Assign an empty array to an existing array like this: “let arr = []”. Method 2: Setting an “array length to 0“. Method 3: Using the “splice()” method. Method 4: Using the “pop()” method. Method 1: Assigning an empty array … Read more