Check if a Parameter is provided to Function in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
2 min

banner

# Check if a Parameter is provided to Function in JavaScript

To check if a parameter is provided to a function, use the strict inequality (!==) operator to compare the parameter to undefined.

If the comparison returns true, then the parameter was provided to the function.

index.js
function getParam(param) { if (param !== undefined) { console.log('The parameter was provided to the function'); return param; } } console.log(getParam(5)); // ๐Ÿ‘‰๏ธ 5 console.log(getParam()); // ๐Ÿ‘‰๏ธ undefined

check if parameter is provided to function

The code for this article is available on GitHub

We used the strict inequality (!==) operator to check if the value of the parameter is not equal to undefined.

If the condition passes, then the parameter is provided to the function.

Note that the if block wouldn't run if you explicitly pass an undefined value to the function.
index.js
function getParam(param) { if (param !== undefined) { console.log('The parameter was provided'); return param; } else { // ๐Ÿ‘‡๏ธ this runs console.log('The parameter was not provided'); } } console.log(getParam(undefined)); // ๐Ÿ‘‰๏ธ undefined

explicitly passing undefined to the function

The code for this article is available on GitHub

I can't think of a good use case to do this, so I don't see this as a drawback.

If any other value is passed to the function, the if block runs.

# Check if a Parameter is provided to Function using typeof

Alternatively, you can use the typeof operator.

If the condition is met, the parameter is provided to the function.

index.js
function getParam(param) { if (typeof param !== 'undefined') { return param; } } console.log(getParam(5)); // ๐Ÿ‘‰๏ธ 5 console.log(getParam()); // ๐Ÿ‘‰๏ธ undefined

check if parameter is provided to function using typeof

The code for this article is available on GitHub

We used the typeof operator to get a string that indicates the type of a value.

Here are some examples of using the typeof operator.

index.js
console.log(typeof undefined); // ๐Ÿ‘‰๏ธ "undefined" console.log(typeof (() => {})); // ๐Ÿ‘‰๏ธ "function" console.log(typeof function () {}); // ๐Ÿ‘‰๏ธ "function" console.log(typeof null); // ๐Ÿ‘‰๏ธ "object" console.log(typeof []); // ๐Ÿ‘‰๏ธ "object" console.log(typeof {}); // ๐Ÿ‘‰๏ธ "object" console.log(typeof ''); // ๐Ÿ‘‰๏ธ "string" console.log(typeof 0); // ๐Ÿ‘‰๏ธ "number"

When the typeof operator is used on an undefined value, it returns the string "undefined", which is exactly what we check for in our if statement.

# Assigning default values for function parameters

You can assign a default value for the parameter if there is a value that suits your use case.

index.js
function getParam(param = 'hello') { return param; } console.log(getParam()); // ๐Ÿ‘‰๏ธ hello console.log(getParam('world')); // ๐Ÿ‘‰๏ธ world

assigning default values for function parameters

The code for this article is available on GitHub

We used a default value of the string hello for when the function is invoked without a parameter.

If the function gets called and a value for the parameter is not provided, the default value is used.

The default value is also used if the function gets passed a value of undefined.

index.js
function getParam(param = 'hello') { return param; } console.log(getParam(undefined)); // ๐Ÿ‘‰๏ธ hello console.log(getParam('world')); // ๐Ÿ‘‰๏ธ world

If the function gets called with a value for the parameter, the supplied value is used.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev