I have below function in TypeScript (which i can't change the order of the parameters),
user(firstName: string, lastName?: string, address?: string);
When in the case where I need to only pass firstName
and address
what is the better/suitable/recommended value to pass as the lastname
?
case 1: user("Jack", undefined, "NY");
or
case 2: user("Jack", null, "NY");
What are the pros and cons of each approach ?
You should use undefined.
This:
lastName?: string
tells us that lastName
parameter has type of string | undefined
. So using null
would be illegal (in strict mode, which is kinda good). Use undefined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With