#what's and why use block-scoped const in js

3 messages · Page 1 of 1 (latest)

coarse sigil
#

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!

const navMenu = document.getElementById('nav-menu'),
      navToggle = document.getElementById('nav-toggle'),
      navClose = document.getElementById('nav-close')

/*===== MENU SHOW =====*/
/* Validate if constant exists */
if(navToggle){
    navToggle.addEventListener('click', () =>{
        navMenu.classList.add('show-menu')
    })
}

/*===== MENU HIDDEN =====*/
/* Validate if constant exists */
if(navClose){
    navClose.addEventListener('click', () =>{
        navMenu.classList.remove('show-menu')
    })
}
remote ledge
#

You want variables in a scope and not global to help organize your code. X is only needed in Y block so you put X in that block so you can find it more easily.

You also don't want to have too many names in your global scope to avoid naming conflicts.

hushed nexus
#

to avoid naming conflicts just name everything like "AA" AB" "AC" AD" and so on (this is for jokes, if you do this, you're a menace)