1

I just would like to get the confirmation of the thesis in the subject. It's not possible to somehow create local scope, i.e. local variables inside a function like x => some_expression, in contrary to a function like x => {/*any code*/}, is it?

Subquestion: does lexical scope can only be created between curly braces? e.g. in blocks, conditions, loops, try/catch etc.

P.S. I am 99% sure of that, but I want to know maybe I missed something...

Nurbol Alpysbayev
  • 19,522
  • 3
  • 54
  • 89
  • "Does lexical scope can only be created between curly braces?" One exception to the rule is single line blocks where braces are optional. https://stackoverflow.com/questions/1047454/what-is-lexical-scope – Avin Kavish Jun 19 '19 at 16:40
  • 1
    can you examplify @AvinKavish – Shubham Dixit Jun 19 '19 at 16:41
  • @AvinKavish Agreed with Shubh, can you please give an example, I am not quite understand the case you are writing about. – Nurbol Alpysbayev Jun 19 '19 at 16:42
  • I am bit confused here with the local scope and lexical scope ,are you relating them ?@NurbolAlpysbayev. What you are terming is block scope ,not lexical scope,I guess – Shubham Dixit Jun 19 '19 at 16:43
  • @Shubh Aren't they the same? Local scope is a lexical scope, but the opposite is not true. – Nurbol Alpysbayev Jun 19 '19 at 16:44
  • The arrow function mechanic has no difference in terms of scope. It just does not create this, args, etc. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions – Avin Kavish Jun 19 '19 at 16:49

1 Answers1

2

Creating local scope via new variables is not possible for a "concise body" arrow function because there is no way to define a new variable inside of an expression.

If you were to write const func = x => const a or const func = x => const a = x it would fail.