0

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.

Vitamin
  • 11
  • 3
  • The `undefined` is what `console.log` returns. You get this when you type the code in the developer console, which *evaluates* what you type, also `console.log`. Just ignore it, and run your script in a proper environment. – trincot Sep 16 '22 at 07:21

0 Answers0