DEV Community

Discussion on: 3 Ways to Set Default Value in JavaScript

Collapse
mustafaabobakr profile image
Mostafa Abobakr

We can make a helper function

export function defaultValue(VAL, DEFAULT) {
  switch (VAL) {
    case 'null':
    case 'undefined':
    case null:
    case undefined:
      return DEFAULT;
    default:
      return VAL;
  }
}

const a = defaultValue( $(el).val() , 'ok');  // a = 'ok' .... if   $(el).val() === to any case  👆