0

So here I am having let and function param with same name which throws an error can somebody help me with whats happening around?

function foo(x, y) {
  var x = 10; // Allowed
  let y = 5;  // SyntaxError: Identifier 'y' has already been declared
  return x + y;
}
Hiral
  • 119
  • 1
  • 8
  • 1
    why you're defining the variables with same name ? when you have those names already in arguments ? and here point is var allow you to define the same variable again whereas let don't allow re-deceleration of same variable – Code Maniac May 27 '19 at 11:22
  • 1
    Function parameters are mostly like having a variable declared with `var` at the top of the block. A duplicate `var` declaration of a variable name already used is permitted, but a duplicate `let` declaration of a variable name already used is not – CertainPerformance May 27 '19 at 11:24
  • The variables declared with cannot be re-declared. but with var, It will overwrite the old one – Shareef May 27 '19 at 11:26
  • yeah @Shareef That's the fact I am keen about what's happening under the hood – Hiral May 27 '19 at 12:01
  • @CodeManiac I know it's bad practice but as I mentioned I was more interested in the explanation why it's doing so – Hiral May 27 '19 at 12:02
  • @Hiral i have given explanation in my comment why is happening so `here point is var allow you to define the same variable again whereas let don't allow re-deceleration of same variable` – Code Maniac May 27 '19 at 12:04
  • 1
    @Hiral And there comes TDZ (Temporal Dead Zone) for all those ES6 declarations which doesn't let you re-declare or use before their declarations. – Madhu Dollu May 27 '19 at 12:23
  • Thanks @MadhusudhanDollu read few articles about TDZ it explains it well. – Hiral May 27 '19 at 12:32

0 Answers0