How to ignore 'undefined' in memoize function

Viewed 12

For some reason I'm getting undefined and I don't know from where. Can you explain to me where this undefined comes from and how to ignore it?

This is my code

function memoize(cb) {
    const memo = new Map();
    return (...args) => {
        const key = JSON.stringify(args);
        if (!memo.has(key)) {
            memo.set(key, cb(...args));
            console.log("set cach");
        }
        console.log("use cach");
    };
}

const callback = (...args) => args;
// const callback = (val) => val * 2;
const memoized = memoize(callback);
console.log(memoized(123)); // calls callback, returns 123
console.log(memoized(123)); // returns 123
console.log(memoized(123, "abc")); // calls callback, returns [123, 'abc']
console.log(memoized(123, "abc")); // returns [123, 'abc']

and this is the result

set cach
use cach
undefined
use cach
undefined
set cach
use cach
undefined
use cach
undefined

Thank you in advance.

0 Answers
Related