2

I am new to JS and came across this code:

let cache={};

function memoizedAddTo80(n) {
    if (n in cache) {
        return cache[n]
    } else {
        cache[n]= n+80;
        return cache[n]
    }
}

The question is what is cache[n]?, I mean, why do we use [n] after cache. Is cache[n] equal to cache.n Or???

Antoine Delia
  • 1,728
  • 5
  • 26
  • 42
Dickens
  • 895
  • 3
  • 11
  • 25
  • Dot notation and bracket notation are generally equivalent. I suspect that most JS devs use dot notation except when variables are used, in which case bracket notation is necessary. – isherwood Oct 09 '19 at 13:38
  • `Is cache[n] equal to cache.n Or???` yes – Jaydip Jadhav Oct 09 '19 at 13:38
  • 9
    @JaydipJadhav no, it's not (at least not always). `let foo = "bar"; cache.foo !== cache[foo];`. – zero298 Oct 09 '19 at 13:39
  • `Is cache[n] equal to cache.n Or???` not exactly `cache.n === cache["n"]` whereas the `n` in `cache[n]` is a variable, it can contain other values than `"n"` – Thomas Oct 09 '19 at 13:40
  • 3
    Possible duplicate of [Dot and Square Bracket Notation](https://stackoverflow.com/questions/44417664/dot-and-square-bracket-notation) or this [JavaScript property access: dot notation vs. brackets?](https://stackoverflow.com/q/4968406/691711) – zero298 Oct 09 '19 at 13:43

3 Answers3

6

n is a variable. Consider:

var n = "foo";
return cache[n];

This would be equivalent to cache.foo

SpoonMeiser
  • 19,918
  • 8
  • 50
  • 68
  • thank you for your kind answer, just a question, the above code is written in accordance with memoization. So, is it true that memoization basically helps us to optimize the performance of our app? – Dickens Oct 09 '19 at 13:55
  • @Dickens memoization is a technique to reduce the number of computations needed. Let's say you have some function that might need 1 second to derive an answer `compute(3)` will take 1 second and calling `compute(3)` a second time will take another second. With memoization, only the first call is computed and then cached, so `compute(3); compute(3)` will take a total of 1 second, not 2 seconds. And if you have repeat calls to it, you can save up even more time. Calling `compute(5)` will still trigger another 1 second wait, of course. – VLAZ Oct 09 '19 at 14:00
  • @VLAZ, thanks dude for your kind explanation:) – Dickens Oct 09 '19 at 14:18
  • @VLAZ, by the way, sorry :D, is it true that memoization is connected with data structures, I mean, after compute(3) is finished executing it then puts result into data structure e.g array. And when we execute compute(3) the second time it checks if such result is in that array, if there is then execute if not then not execute. Is it correct? – Dickens Oct 09 '19 at 14:25
  • 1
    It puts the result in an object, not an array. But otherwise, that's correct. Memoization builds up a bunch of links of `input -> output`. So, if `compute(3)` returns, say, `42`, then you get `3 -> 42` and so if you get `3` again, you just return `42` immediately. It works with [pure functions](https://en.wikipedia.org/wiki/Pure_function) - functions that don't have side-effects. If, for example if `compute` checks the current date and time, that's "impure" since calling `compute(3)` times at different times will not produce the same output. – VLAZ Oct 09 '19 at 14:37
2

You can review this code below:

var key = 'a'

var obj = {
    a: '1',
    key: '2'
}

console.log(obj[key])  // output '1' because it same as obj.a
console.log(obj.key)  // output '2'
nnfans
  • 131
  • 1
  • 9
0

n is a string argument.

You can use it in KEY syntax obj['key'],

but you can't get obj.key property with a dot notation because it does not exist.

Andrii Verbytskyi
  • 7,155
  • 3
  • 47
  • 38