Logo

dev-resources.site

for different kinds of informations.

Javascript default parameter for null and undefined

Published at
11/11/2021
Categories
javascript
null
undefined
parameter
Author
sajidurshajib
Author
13 person written this
sajidurshajib
open
Javascript default parameter for null and undefined

Hello Devs,
Are you facing any problem when you pass null or undefined in a function as a parameter? May be you have faced this problem or maybe you haven't.On the other hand, maybe you know the solutions in many ways.

In this post I want talk about how to pass default value in case of null and undefined. Hopefully, it will help somebody.

Oh Enough Introduction...

Let's get to the main point. Take a function called sayHi() which takes a parameter and print that parameter. that's it.

const sayHi = (greeting) => {
    console.log(greeting)
}

sayHi('Hi')

//your@console:~$ Hi
Enter fullscreen mode Exit fullscreen mode

Now, if I don't give any parameter what will be happen? Let's check...

const sayHi = (greeting) => {
    console.log(greeting)
}

sayHi()

//your@console:~$ undefined
Enter fullscreen mode Exit fullscreen mode

So in this case if we set a default parameter in the function,our problem will be solved.

const sayHi = (greeting='Hi') => {
    console.log(greeting)
}

sayHi()

//your@console:~$ Hi
Enter fullscreen mode Exit fullscreen mode

So undefined related problem solved...!

Now ,let's check again if I put a null parameter what will happen?

const sayHi = (greeting='Hi') => {
    console.log(greeting)
}

sayHi(null)

//your@console:~$ null
Enter fullscreen mode Exit fullscreen mode

Ok, that's the problem we don't need a null value as our output. Default value will be used when the parameter is undefined. However, if we put null here, our default parameter can't prevent it. Because undefined!==null

So how should we handle this?

Now we need nullish coalescing operator ??

console.log(12 ?? "not found") // 12
console.log(0  ?? "not found") // 0

console.log("Sajid" ?? "not found") // "Sajid"
console.log(""     ?? "not found") // ""

console.log(true  ?? "not found") // true
console.log(false ?? "not found") // false

console.log(undefined ?? "not found") // "not found"
console.log(null      ?? "not found") // "not found"
Enter fullscreen mode Exit fullscreen mode

If the left value is null or undefined , then the right value will be assigned. That's how nullish coalescing operator works.

So out solution will be

const sayHi = (greeting) => {
    const hi = greeting ?? 'Hi'
    console.log(hi)
}

sayHi(null)

//your@console:~$ Hi
Enter fullscreen mode Exit fullscreen mode

You can also use || or operator which can be problematic if your left value contains "" or 0 or false

Something like that,

console.log("" || "not found") // "not found"
console.log(0 || "not found") // "not found"
Enter fullscreen mode Exit fullscreen mode

So coalescing operator is our best solution if we want to ignore only undefined and null as parameter.

So Dev, what do you think about this operator?

null Article's
30 articles in total
Favicon
How Imburse Payments Ships High-Quality APIs Faster
Favicon
Need to Verify Your JSON Schema? Here's a Few Ways to Do It!
Favicon
Code Smell 260 - Crowdstrike NULL
Favicon
Be careful when using NULL in PostgreSQL
Favicon
The most painful reason NULLs are evil
Favicon
Null or Nothing? Unmasking the Mystery of Parameters in Dart
Favicon
La Solución del Billón de Dólares
Favicon
11 Lessons to learn when using NULLs in PostgreSQL®
Favicon
NULLs Are Not The Same – A Guide
Favicon
the (not so big) Bang!
Favicon
Rust's Option type... in Python
Favicon
Understanding Nullable Reference Types in C#
Favicon
Working with NULL in Databases. Turn Your Frustration Into Delight
Favicon
TypeScript: The many types of nothing
Favicon
ERROR: null" or "null pointer exception while invoking FlowService - Storage get operation
Favicon
ServiceNow: 1 thing for safer GlideRecord scripts
Favicon
How NullPointerException can be avoided in Java
Favicon
Consider these facts when dealing with NULL in RDBMS
Favicon
Unhandled Exception: type 'Null' is not a subtype of type 'int' in type cast error when trying to call function with no int
Favicon
When <nil> is not <nil>
Favicon
Absence of null in Solidity
Favicon
How to Check for Null in Javascript
Favicon
Javascript Tagalog - Null
Favicon
Kotlin 基礎 Part 1 -- !! や ?: と ?.let で Nullable な値を処理する
Favicon
Remove null check, use the Optional
Favicon
Handling null: optional and nullable types
Favicon
More JS Concepts
Favicon
Valores null e undefined no JavaScript
Favicon
Javascript default parameter for null and undefined
Favicon
How I Learned to Stop Worrying and Love NULL in SQL

Featured ones: