DEV Community

loading...

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

VICTOR OMONDI
Data Scientist | Graphics & UI/UX Designer | @DSC_MMU Lead | @MoringaSchool, @Ingressive & @CodeLN Campus Ambassador
・1 min read

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

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

Discussion (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