Caching is a way to store values so you can use them later on.
Memoization is a specific form of caching that involves caching the return value of a function based on its parameters.
// Without cachingfunctionaddTo80(n) {console.log("very long calculation");return n +80;}addTo80(5);// With caching - without closurelet cache = {};functionmemoizeAddTo80(n) {if (n in cache) {// similar to cache.nreturn cache[n]; } else {console.log("very long calculation");constanswer= n +80; cache[n] = answer;return answer; }}console.log(1,memoizeAddTo80(6));console.log(cache);console.log("-----------");console.log(2,memoizeAddTo80(6));// With caching and closure - the idea is to put cache inside the function and use closure to remember the cachefunctionmemoizeAddTo80() {let cache = {}; //without return function below, everytime we run, we will get empty cache objectreturnfunction(n) {if (n in cache) {return cache[n]; } else {console.log("long time");constanswer= n +80; cache[n] = answer;return answer; } };}constmemoized=memoizeAddTo80(); // must put this to remember the cache valueconsole.log(1,memoized(6));console.log(2,memoized(6));