Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why constant variables for selecting DOM elements? [duplicate]

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');
like image 849
Arvind kumar Avatar asked Dec 02 '25 03:12

Arvind kumar


1 Answers

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.

like image 82
waterplea Avatar answered Dec 04 '25 16:12

waterplea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!