loading...

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

victoromondi1997 profile image VICTOR OMONDI ใƒป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

pic
Editor guide
 

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

w3schools.com/js/js_strict.asp