JavaScript Math round() Method

JavaScript Math round() method is used to round a number to the nearest integer. If the number is less than or equal to 7.49, it will be rounded to 7, and if it is 7.50 or greater, it will be rounded to 8. In the case of a negative number, it behaves differently. Syntax Math.round(x) … Read more

How to Remove an Element from Array in JavaScript

Here are four ways to remove an element from an array in JavaScript: Using array.pop() Using array.shift() Using delete operator and splice() Using filter() and indexOf() along with the splice() Method 1: Using array.pop() The array.pop() method can help you remove the last element of the array in JavaScript. const main_array = [11, 21, 18, 19, … Read more

JavaScript String toUpperCase() Method

JavaScript String toUpperCase() method is used to convert a string to uppercase letters and returns a new string with all characters in uppercase. It does not change the original string. This method throws a TypeError when called on null or undefined. Syntax string.toUpperCase() Parameters None. Return Value It returns a new string where all characters are in uppercase. Visual Representation Example … Read more

JavaScript Array find() Method

JavaScript Array find() method is used to get a value of the first element in the array that satisfies the provided test function. If no element is satisfied, it returns undefined. Syntax array.find(function(element, index, arr),thisValue) Parameters function(required): This is a function that executes on each element of the array. element(required): This is the current item … Read more

How to Get All Unique Values (Remove All Duplicates) from Array in JavaScript

Here are five ways to get all unique values(remove all duplicates) from an array in JavaScript: Using new Set() constructor Using filter() + indexOf() Using filter() Using Set and Array.from() Defining custom Array Unique Prototype Method 1: Using the new Set() constructor Use the “[…new Set(arr)]” expression, where you pass the array to the Set() constructor, … Read more

How to Remove First Element from Array in JavaScript

Here are three ways to remove the first element from an array. Using the shift() method Using the splice() method Using the filter() method Method 1: Using the array.shift() method Visual Representation let arr = [‘Jonas’, ‘David’, ‘Ulrich’, ‘Adam’, ‘Eva’] removedElement = arr.shift(); console.log(‘Removed Element: ‘, removedElement); console.log(‘Remaining array’, arr); Output Removed Element: Jonas Remaining … Read more

JavaScript Object keys() Method

JavaScript Object.keys() method is used to return an array of a given object’s own enumerable property names. This method is commonly used for iterating over an object’s properties. Syntax Object.keys(obj) Parameters obj(required): An iterable object. Return Value It returns an array of strings representing all the enumerable properties of the given object. Visual Represenatation Example … Read more

JavaScript Array reduce() Method

JavaScript Array reduce() method is used to execute a reducer function on each element of the array and returns a single output value. This method does not change the original array. Syntax array.reduce(function(accumulator, currentValue, currentIndex, arr), initialValue) # or array.reduce(callback[, initialValue]); Parameters function(required): Each array element is a required parameter. It contains four parameters which are listed … Read more

JavaScript Object assign() Method

The Object assign() method is used to copy all own enumerable properties of one or more source objects to a target object.  This method is commonly used for cloning objects or merging multiple objects into a single object. If a source value is undefined or null, it is ignored and not copied. Syntax Object.assign(target, …sources) Parameters … Read more

JavaScript Object values() Method

The Object values() method is used to return an array of the given Object’s enumerable property values. Syntax Object.values(obj) Parameters obj: It is an object whose enumerable property values will be returned. Return Value Returns its own enumerable property’s values as an array. Visual Representation Example 1: How to Use Object.values() method let obj = … Read more

JavaScript delete Operator

JavaScript delete operator is used to remove the property from the object.  It does not delete variables or functions. However, deleting an object property that doesn’t exist will not affect the object but will return true.  It can be useful for dynamic manipulation of objects where certain properties may no longer be needed or applicable. … Read more

How to Add Property to an Object in JavaScript [5 Ways]

Here are five ways to add a property to a JavaScript Object: Using “dot(.) notation” Using “square brackets” Using “Object.defineProperty()” function Using “Object.assign()” function Using “Spread operator” Method 1: Using dot(.) notation Syntax object.new_property = new_value; Visual representation Example let obj = { name: ‘Krunal’, age: 27, education: ‘Engineer’ }; console.log(obj) obj.college = ‘VVP’; console.log(‘After adding … Read more

JavaScript Promise all() Method

JavaScript Promise all() method takes an iterable of promises as input and returns a single Promise that resolves when all of the promises in the iterable have resolved, or rejects as soon as one of the promises in the iterable rejects. Syntax Promise.all(iterable); Parameters iterable: It is an Object such as an Array. Return Value … Read more

JavaScript JSON parse() Method

JSON.parse() method is used to parse a JSON string and returns a JavaScript object.  Syntax JSON.parse(text, reviver) Parameters text(required): This is the string to be parsed as JSON. reviver(optional): A function that can transform the object values before they are returned. The reviver function is called for each member of the object. If a member contains … Read more

JavaScript Fetch API: How to Make GET and POST Requests

Javascript fetch API is “used to provide an interface for accessing and manipulating parts of the protocol, such as requests and responses.” Syntax let promise = fetch(URL, [options]) Parameters URL: The URL to access. options: – Optional parameters: method, headers, etc. Without options, that is the simple GET request, downloading the contents of the url. … Read more

JavaScript Array includes() Method

JavaScript Array includes() method is used to check if an array contains the specified element. Syntax array.includes(element, fromIndex) Parameters element(required):  It is the element that we need to search for. fromIndex(optional): It is the position in the array to start the search. (Default is 0) Return Value Returns true if element found, otherwise false. Visual … Read more

JavaScript String trim() Method

JavaScript String trim() method is used to remove whitespace from both ends(start and end) of a string and returns a new string without modifying the original string. Whitespace characters include space, tab, no-break space, etc. Syntax string.trim() Parameters None. Return Value It returns the new String with removed whitespace from both ends. Visual Representation Example … Read more

How to Remove a Character From String in JavaScript

Here are four ways to remove a character from a string in JavaScript: Using string.replace() Using string.replace() with the regex Using string.slice() Using string.substr() Method 1: Using string.replace() The string.replace() method replaces a specific character with an empty character. Syntax string.replace(‘character_to_replace’, ”) Visual representation Example string = “AppDividend”; str_chr_removed = string.replace(‘A’, ”); console.log(‘Original String: ‘, string); console.log(‘After … Read more

JavaScript String search() Method

JavaScript String search() method is used to search for a match between regular expressions and a provided string object. String search() is case-sensitive. Syntax string.search(searchvalue) Parameters searchValue(required): It is a regular expression.  The string will be converted to the regular expression. Return value  Returns the index (position) of the first match. If the pattern is … Read more

How to Import Modules in JavaScript

The “import” keyword in JavaScript is part of the ES6 (ECMAScript 2015) used to include functions, objects, or values from other modules into your current module or script. This is beneficial for code organization, separation of concerns, and reusability. Syntax The syntax of the import module is the following. It has many variations because it depends on whether we … Read more

JavaScript Math log() Method

The JavaScript Math log() method is used to calculate the natural logarithm (base e) of a number. Syntax Math.log(x) Parameters x(required):  It is a number whose natural logarithm is to be determined. Return value The natural logarithm of x. If a negative argument is passed, the method returns NaN. If the passed value is not … Read more

How to Convert an Array to JSON in JavaScript

The JSON.stringify() function is used to convert JavaScript object, array, or value to a JSON string. Syntax JSON.stringify(value, replacer, space) Example 1: Convert an Array to JSON Visual Representation let arr = [ ‘krunal’, ‘ankit’, ‘rushabh’ ] jsonData = JSON.stringify(arr) console.log(jsonData) console.log(typeof jsonData === ‘string’) Output [“krunal”,”ankit”,”rushabh”] true Example 2: Array to JSON with indexes … Read more

JavaScript Array some() Method

JavaScript Array some() method is used to check whether at least one element in an array passes the given test function. This method does not modify the original array. Syntax array.some(callback(element, index, arr), thisValue) Parameters function(required): It is a function that tests each element of the array. It can take up to three arguments: element(required): … Read more

JavaScript Math floor() Method

The Math floor() method is used to round a number down to the nearest integer. Syntax Math.floor(x) Parameters x(required): It is the number whose floor value is to be calculated. Return Value Returns the largest integer less than or equal to a provided number If no argument is passed, returns negative NaN. If arguments cannot … Read more

JavaScript String lastIndexOf() Method

JavaScript String lastIndexOf() method is used to find the last occurrence of a specified substring within a string. Syntax string.lastIndexOf(searchvalue, index) Parameters searchValue(required): It is the string we are looking for. index(optional): It is the index at which to start the search backward. If index >= string. length, the entire string is searched. If index < … Read more

JavaScript Math pow() Method

JavaScript Math pow() method returns the base number raised to the power of the exponent number. This method is particularly useful in scientific calculations, exponential growth calculations, and other mathematical operations involving powers. Syntax Math.pow(x, y) Parameters x(required): The base of the power. y(required): The exponent to which the base is raised. Return Value The value … Read more

JavaScript Math sin() Method

JavaScript Math sin() method is used to calculate the sine of a number. The input number is expected to be in radians, not degrees. Syntax Math.sin(x) Parameter(s) x(required): It is a number whose sine value is to be determined. Return Value Returns a numeric value between -1 and 1 representing the sine of the angle … Read more

JavaScript Math min() Method

The JavaScript Math min() method returns the smallest value among the numbers provided as arguments. Syntax Math.min(x, y, z, …) Parameters x, y, z,…: The numbers out of which the minimum is to be determined. Return Value Returns the smallest of the given numbers. If no argument is passed, this method returns Infinity. If any … Read more

JavaScript Math cos() Method

The Math cos() method is used to calculate the cosine of a number. The input number should be in radians, not degrees. Syntax Math.cos(x) Parameters x(required): It is a number whose sine value is to be determined. Return Value Returns a numeric value between -1 and 1. If the parameter is not numeric or if … Read more

JavaScript Math atan2() Method

JavaScript Math atan2() is used to calculate the arctangent of the quotient of its arguments. Syntax Math.atan2(y,x) Parameters y: The y coordinate of the point. x: The x coordinate of the point. Return Value The arctangent quotient is in the range from -pi to pi radians. It returns NaN for non-numeric arguments and empty. Visual … Read more

JavaScript Math trunc() Method

The Math trunc() method is used to remove the fractional part of a number, leaving only the integer part. Syntax Math.trunc(number) Parameters number(required): The floating-point number whose integral part is to be removed. Return Value Returns the integer part of the given number. If you pass a non-numeric value, it returns NaN. Visual Representation Example 1: … Read more

JavaScript Math atan() Method

JavaScript Math atan() method is used to calculate the arctangent of a number. Syntax Math.atan(num) Parameters num(required): It is a number whose arctangent you want to calculate. Return value Returns the numeric value between -pi/2 and pi/2 radians. If the passed value is empty or not numeric, the method returns NaN. Visual RepresentationExample 1: How to Use … Read more

JavaScript Math cbrt() Method

The Math cbrt() method is used to calculate the cube root of a number. Syntax Math.cbrt(number) Parameters number(required): It is a number whose cube root you want to calculate. Return Value Returns cube root value of a number If you pass a non-numeric or empty value, it returns NaN. Visual Representation Example 1: How to Use … Read more

JavaScript Math tanh() Method

JavaScript Math tanh() method is used to calculate the hyperbolic tangent of a number. This method is particularly useful in scientific computations, especially those involving hyperbolic functions in fields like physics, engineering, and certain areas of mathematics. Syntax Math.tanh(x) Parameters x(required): It is a number whose hyperbolic tangent value is to be determined. Return Value … Read more

JavaScript Math tan() Method

The JavaScript Math tan() method is used to calculate the tangent of a number in radians. The tangent of an angle in a right-angled triangle is the ratio of the length of the opposite side to the length of the adjacent side. Syntax Math.tan(number) Parameters number(required): It is the angle in radians for which you … Read more

JavaScript Math atanh() Method

JavaScript Math atanh() method is used to calculate the hyperbolic arctangent (inverse hyperbolic tangent) of a number. Syntax Math.atanh(num) Parameters num(required): It is a number whose hyperbolic arctangent value is to be determined. Return Value It returns the hyperbolic arctangent value. If the passed value is outside the range [-1,1], the method returns NaN. If the … Read more

JavaScript Math asin() Function

JavaScript Math asin() method is used to calculate the arcsine(in radians) of a number of a number. Syntax Math.asin(x) Parameters x(required): It is the number for which you want to calculate the arcsine. The number should be in the range of -1 to 1. Return Value Returns a numeric value between -pi/2 and pi/2 radians. … Read more

JavaScript Math asinh() Method

The Math asinh() method is used to calculate the hyperbolic arc-sine of a number. The hyperbolic arc-sine is known by other names such as inverse hyperbolic sine. Syntax Math.asinh(x) Parameters x: It is a number whose hyperbolic arcsine value is to be determined. Return Value Returns the inverse hyperbolic sine (asinh) of the number you … Read more

JavaScript Math sinh() Method

JavaScript Math sinh() method is used to calculate the hyperbolic sine of a number. Formula 𝙼𝚊𝚝𝚑.𝚜𝚒𝚗𝚑(𝚡)=sinh(x )=ex−e−x2 Syntax Math.sinh(num) Parameters num(required): It is the number for which you want to calculate the hyperbolic sine. Return Value Return the hyperbolic sine of a number. If the passed value is non-numeric or empty, the method returns NaN. … Read more

JavaScript Math cosh() Method

JavaScript Math cosh() method is used to calculate the hyperbolic cosine of a number. Syntax Math.cosh(num) Parameters num(required): It is the number whose hyperbolic cosine value will be determined. Return Value Returns the hyperbolic cosine of a number. If the passed value is non-numeric or empty, the method returns NaN. Visual Representation Example 1: How to Use … Read more

JavaScript Math acos() Method

JavaScript Math acos() method is used to calculate the arccosine(in radians) of a number. Syntax Math acos(x) Parameters x(required): It lies between -1 and 1, whose arccosine value will be determined. Return Value Returns value between 0 and PI. If the passed value lies outside the range [-1,1], the method returns NaN. If the passed … Read more

JavaScript Math acosh() Method

JavaScript Math acosh() method is used to calculate the hyperbolic arc-cosine(inverse hyperbolic cosine) of a number. Syntax Math.acosh(x) Parameters x(required): It is the number whose hyperbolic arccosine value will be determined. A number must be greater than or equal to 1. Return Value It returns the hyperbolic arccosine value. If the passed value is less … Read more

JavaScript String match() Method

JavaScript String match() method is used to get the result of matching a string against a regular expression. Syntax string.match(regexp) Parameters regexp(required): It is a value to search for as a regular expression. Return Value It returns an array containing all of the matches, or null if no match is found. Visual Representation Example 1: How to … Read more

How to Check If a Variable is “undefined” in JavaScript

To check if a variable is undefined in JavaScript, use the “strict equality operator(===)” or “typeof operator”. If a variable has been declared but not assigned a value, it is automatically assigned the value undefined. Method 1: Using the strict equality operator(===) Visual Representation let myVar; let x = 10; if (myVar === undefined) { console.log(‘myVar … Read more

JavaScript Promise then() Method

JavaScript Promise.then() method is called when a promise is resolved. Syntax promise.then(onFulfilled[, onRejected]); promise.then(value => { // fulfillment }, reason => { // rejection }); Parameters It takes two callback function parameters: onFulfilled(required): A function to be executed if the promise is resolved successfully. onRejected(optional): A function to be executed if the promise is rejected. … Read more

JavaScript Map forEach() Method

The Map forEach() method is used to execute a given function once per each key/value pair in the Map object. Syntax map.forEach(callbackFn(value, key, map){}, thisArg); Parameters callback(required): This is a callback function that executes each function call. value: The value of the current Map entry. key: The key of the current Map entry. map: The … Read more

JavaScript Array Sort() Method

JavaScript Array sort() method sorts the elements of an array either by the default sorting order or using a custom sorting function. By default, it sorts the array elements as strings in ascending order based on their string Unicode values. Syntax array.sort(compareFunction) Parameters compareFunction(optional): The function defines an alternative sort order, and it should return a … Read more