4

Is there a reason as to why we declare the variables as constant while selecting elements from the DOM? I see a lot of devlopers and tutorials use

const form = document.querySelector('form');
Andy
  • 61,948
  • 13
  • 68
  • 95
  • Closed as a false positive. The specific focus of the question merits more attention. Voted to re-open. – Rounin Nov 19 '19 at 14:21
  • Hello, the question is misleading anyway, because it makes an assumption that when you select an element from the DOM, it's better to use const. I think this is great that it's pointed to the right question asking the difference between const and let, keeping close. – Baptiste Pernet Nov 19 '19 at 18:20

1 Answers1

1

You want to store the result so you do not have to query it again. And it is also best practice to use const as often as you can so you cannot accidentally reasign it. In case you DO need to reasign it, it is preferable to use let.

waterplea
  • 3,462
  • 5
  • 31
  • 47