➡️➡️ Double Arrow Function || One-Line || Explained
Approach\nThis problem involves the concept of a closure.\n\nA closure is the combination of a function and a reference to the function\'s outer scope.\n\nIt\'s
# Approach\nThis problem involves the concept of a closure.\n\nA closure is the combination of a function and a reference to the function\'s outer scope.\n\nIt\'s the ability of an inner function to access its outer scope even after the outer function has finished executing.\n\nIn this problem, the closure is the pairing of the function being returned inside of createCounter and its access to the outer scope including createCounter\'s variables. \n\nWhen createCounter has finished executing, the inner function is still able to access createCounter\'s variables. \n\nEach time the inner counter function is called, it accesses createCounter\'s parameter n, returns its value, and updates it.\n\nIt\'s important to use post-increment (i++) operator here since the problem specifies that the first call returns n.\n```\nvar createCounter = function(n) {\n    return function() {\n        return n++\n    };\n}\n```\n\nIf closures still confuse you, I\'d recommend looking up "closures javascript" on google. There\'s tons of great resources (ie. YT videos, StackOverflow posts) that explain this concept in more detail.\n### Arrow Function Syntax\nWe can make the syntax more concise by using arrow functions.\n\nArrow functions provide an alternate way of writing functions. They typically follow one of the following formats\n```\n() => expression\nparam => expression\n(param1, param2, ...) => expression\n```\nIn this problem, we can convert both the inner and outer functions into arrow functions.\n\n```\nvar createCounter = n => () => n++\n```\n\nAs mentioned in the comments, there\'s an even shorter version.\n\nWe can use \'_\' to replace the parentheses. We can also omit the var keyword.\n\n```\ncreateCounter = n => _ => n++\n```\nNot sure if there\'s a LC question that you can answer in fewer characters than this lol.
['JavaScript']