How to Check If String is empty/undefined/null in JavaScript

How to Check If String is empty:undefined:null in JavaScript

To check if a string is empty/undefined/null in JavaScript, you can use the “! operator” that returns true for empty strings, undefined values, null values, and other false values. Example 1 function isEmpty(str) { return !str; } const emptyString = ”; const nullValue = null; const undefinedValue = undefined; const nonEmptyString = ‘Hello, world!’; console.log(isEmpty(emptyString)); console.log(isEmpty(nullValue)); … Read more

What is the eval() Function in JavaScript

Javascript eval() Function

JavaScript eval() is a built-in function that “evaluates or executes an argument”. The function accepts a string as an argument, evaluates it as an expression, and returns the result. Syntax eval(string) Arguments string: It takes a string which is a  JavaScript expression, variable, statement, or sequence of statements. Return value The eval() function returns the result … Read more

How to Perform Integer Division in JavaScript

How to Perform Integer Division in JavaScript

To perform integer division by first doing “regular division” and then using “Math.floor()”, “Math.trunc()”, or “bitwise operator” to discard the decimal part of the result. In JavaScript, there is no built-in operator or function precisely for integer division. Method 1: Using the Math.floor() function The “Math.floor()” method “rounds down a number to the nearest integer”. … Read more

Understanding Default Parameters in JavaScript

Understanding Default Parameters in JavaScript

In JavaScript, default parameters are used when a value for a given parameter is not provided or is “undefined”. This feature was introduced in ECMAScript 2015 (ES6) and allows for more concise and readable code. Syntax function functionName(parameter1 = defaultValue1, parameter2 = defaultValue2, …) { // function body } How to set default parameters in … Read more

How to Set a Time Delay in JavaScript

How to Set a Time Delay in JavaScript

To set a time delay in JavaScript, you can use the “setTimeout()” function. The “setTimeout()” function allows you to execute a function or code after a specified delay. Syntax setTimeout(functionToExecute, delayInMilliseconds, arg1, arg2, …); Parameters functionToExecute: The function you want to execute after the specified delay. delayInMilliseconds: The delay in milliseconds before the function is … Read more

How to Check If a Function Exists in JavaScript

How to Check If a Function Exists in JavaScript

To check if a function exists in JavaScript, you can use the “typeof” operator. Example 1 function mainFunction() { console.log(‘Hello, world!’); } if (typeof mainFunction === ‘function’) { mainFunction(); // Calls the function if it exists } else { console.log(‘The function does not exist.’); } Output In this example, typeof mainFunction will return the string … Read more

Does JavaScript Support Array/List Comprehension

As of ES2022 or ESNext, JavaScript does not have built-in support for Array comprehension or List comprehension like Python. However, you can create an array or list comprehension functionality using the “Array.prototype.map()” along with the “Array.prototype.filter()” methods or the “Array.from()” method in JavaScript. Method 1: Using the Array.prototype.map() along with Array.prototype.filter() You can create a new … Read more

5 Different Ways to Copy an Object in JavaScript

5 Different Ways to Copy an Object in JavaScript

There are five different ways to copy an Object in JavaScript. Using the “Object.assign()” method Using the “Spread operator” Using the “JSON.parse()” and “JSON.stringify()” methods Using the “Object.create()” method Deep copy using a “custom function”. Method 1: Using the Object.assign() method Object.assign() copies the values of all enumerable properties from one or more source objects to a target … Read more