what is and why use block-scope const in js?
Hi, there.
Below is the code example I encountered. It is my first time seeing const declared after a const. What are block-scoped const in js and why use them there? What would be the difference if the const declare globally?
Thank you for your attention and help!
6 Replies
This is just multiple declarations "in one line", it is the same as if you would write:
Block scoped
const
on the other hand is when you declare a const
inside a scope (for example a function
)
see:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/consthi Wolle, thanks! I understand now. Didn't know const can be written that way. Can you explain to me what does? is it abbreviated?
If
document.getElementById
(and querySelector/All) can not find the element, they return null
, that is to make sure the HTML elements are there and already loaded.oh, so it mean "if ( there is this element called const navMenu), run the below code", right?
thank you for your help! 🙂
Yes. (correctly it asks if the content of
navToggle
is a truthy value. If you don't know what truthy/falsy is: https://medium.com/coding-at-dawn/what-are-falsy-values-in-javascript-ca0faa34feb4)got it! and thanks for the article, will read about it!