March 24, 2021

A Comprehensively Short Guide to let, var and this

A Comprehensively Short Guide to let, var and this

Back in September, I wrote this story on var, let and the keyword this

However, some people still found it hard to distinguish between the difference — or rather, they wanted something that was more visually accessible. So I decided to change the format a little for today’s piece.

Here is your comprehensively short guide/notes version to let, var and this

var, let and this recap:

  • JavaScript is a lexical language — this means inheritance flow inwards
  • if a variable is declared at the root level, it becomes a global variable and accessible by everything
  • var is scoped to the nearest function block, meaning the closest { } pair that’s attached to a function.
  • let is scoped to the nearest closing block, meaning the closest { } pair, regardless of what it is.
  • this lets you access variables that are outside the closing block and function scopes. Depending on what you used (i.e. let or var), it can change the way results are returned.

And that’s basically var, let and this in nutshell.