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