Warning: session_start(): open(/tmp/sess_d261946b50e97e3caf1a51ad2584ff2e, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Check If Parameter Is Provided To Function In JavaScript - LearnShareIT

How To Check If Parameter Is Provided To Function In JavaScript

Check if Parameter is provided to Function in JavaScript

In this article, you’ll learn how to check if a parameter is provided to a function in JavaScript by checking if the parameter is undefined. Let’s read it now to get more knowledge.

Check if parameter is provided to function in JavaScript

To check if parameter is provided to function in JavaScript, the important thing to know is that if a parameter is not passed into a function, that parameter is considered undefined. That is all.

Multiple ways to check for that:

  • Using a simple Equality (==) or unequality (!=) operator, preferably a strict equality (===) or unequality (!==) operator to avoid falsy values (i.e., false, null and in this case undefined being equal to each other).

Code:

if (param === undefined) {...}

  • Using a Nullish Coalescence (??), which despite its name, does check whether a value is undefined and will perform a single line of code or return a value.

Code:

param ?? ...

Example:

a = param ?? "" // If param is undefined, return a blank string ("")

However, something more pressing would be how to avoid an undefined parameter. Which the following parts will explain.

Using Default Parameters

A Default parameter, as its name implies, sets the parameters value to something if none is provided. For example:

function func(a="LearnShareIT") {
    // The a parameter will be "LearnShareIT" if none is provided
}

While this solution is great in many ways, it isn’t always helpful, depending on the situation.

Ending the Function

If you cannot stomach the idea of a possibly unprovided parameter or the parameter having a default value, it would be best to simply end the function there.

Generally, you could just check if a parameter is undefined with an if statement.

Code:

function func(a) {
	if (a !== undefined) {
		...
	}
}

However, a problem arises when there are too many parameters to check, be it with a bunch of set parameters or with a Rest parameter (i.e., a list with an unknown amount of parameters). For example:

function func(a, b, c, d, e, f, g, h, i, j, k) {
	if (a !== undefined && b !== undefined && ...) {
		...
	}
}

function func(...params) {
	let proceed = true;
	for (const param of params) {
		// If a single parameter is undefined, the function will not be executed
		if (param === undefined) proceed = false;
	}
	if (proceed) {
		...
	}
}

In these cases, you might want to use a try-catch statement, as if a value being undefined causes an error in the function, and the function will simply not do anything.

However, the cases when an error might occur are few, mostly happening due to a parameter being assumed to be an Object and a property of that assumed Object being referred. For example:

Code:

function func(obj) {
	try {
		obj.val = 2; // Error if the obj parameter is undefined
	}
    catch (error) { 
        // Do Nothing
	}
}

Summary

To check if parameter is provided to function in JavaScript, you must check if the parameter is undefined. Additionally, you can avoid the parameter being unprovided by either setting a default value for that parameter or not executing the function altogether.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *