Home > Enterprise >  Difference between return and return null
Difference between return and return null

Time:09-21

Found this function in a project I am working on. And since I am rather new in JavaScript, I was wondering what is the difference between return; at the top, and then return null; at the bottom? Are there any differences or did someone just forget to type null??

  export function validCvr(value: string | number): ValidationResult {
    if (!value) {
      return;  // <-- HERE
    }

    value = value.toString();
    const CVR_REGEXP = /^[0-9]{8}$/;

    if (!CVR_REGEXP.test(value)) {
      return {
        [ValidationError.invalid]: true
      };
    }

    const weights = [2, 7, 6, 5, 4, 3, 2, 1];

    let sum = 0;
    for (let i = 0; i < weights.length; i  ) {
      sum  = weights[i] * parseInt(value.substr(i, 1), 10);
    }

    const isValid = sum % 11 === 0;
    if (!isValid) {
      return {
        [ValidationError.invalid]: true
      };
    }

    return null; // <-- HERE
  }

CodePudding user response:

to simply put it - return; is equal to return undefined;

  • Related