DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

VICTOR OMONDI
VICTOR OMONDI

Posted on

Javascript: Don't miss to put a var/let/const keyword before a variable declaration

If you miss putting a var/let/const keyword before a variable declaration, it becomes global and gets bind to the global object.

Usecase:

function getName() {
  myVariable = 'shub'
  return myVariable;
}

getName()
console.log(myVariable) // shub
Enter fullscreen mode Exit fullscreen mode

myVariable will become global and will be accessible everywhere in the program

Top comments (1)

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

"use strict"? Or in TypeScript, I think you can put strict: true in tsconfig.json

w3schools.com/js/js_strict.asp

18 Useful Github Repositories Every Developer Should Bookmark

18 Useful GitHub repositories every developer should bookmark: everything from learning resources and roadmaps to best practices, system designs, and tools.