Kayote Kayote - 11 days ago 7
Javascript Question

Argument of Different Data-Types

Is it a good idea / practice to do the following:

export const checkAndUpdateCredit = ( total, addback = false ) => {
// here, addback can be an obj or boolean
let value = total - addback.value
};


As per the comment, if the argument of
addback
isn't passed, it defaults to
false
, else an object is required to be passed. Here a single parameter can be either an
object
or a
boolean
. Is this a good practice / acceptable?

Answer

You could use a default value inside of addback

export const checkAndUpdateCredit = (total, addback = { value: 0 }) => {
//                                                    ^^^^^^^^^^^^