A Comprehensively Short Guide to let, var and this

Back in September, I wrote this story [https://medium.com/madhash/how-variable-types-can-determine-the-life-or-death-of-cats-in-javascript-b1cd2f50a8b1] 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

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.

Article by:

Aphinya Dechalert

Aphinya Dechalert

Aphinya is a skilled tech writer with field experience in software development, agile, and fullstack JavaScript. She is a dev advocate and community builder, helping others navigate their dev journey.

More posts...

JavaScript

How To Use filter, find, map, reduce, every, and some in JavaScript

Not everything is an object. When it comes to functional programming, it’s more to do with the processes that get you from point A to point B than how data and groups of business logic are structured. JavaScript is a language that’s prone to imperative patterns because of

JavaScript

Add CSV import to your Node.js app using UseCSV

CSV is convenient. The likes of Excel, Google Docs, spreadsheet export functions, and reporting applications all support CSV in some capacity. CSV works well with large data sets and the major perk is that it can easily be converted to other formats such as XML or JSON. This is where

JavaScript

21 ways to increase your productivity as a developer

It’s easy to fall into a habit of being comfortable. When it comes to coding, productivity can be improved in two ways — getting good at the language you’re working with or upgrading your environment and processes incrementally.

JavaScript

Bad JavaScript Practices That’s Letting Your Code Down

We’ve all done it — but the question is, are you still coding it? JavaScript is easy — especially if you’ve already got programming knowledge and experience through another language. Many newbies and veteran developers encounter JavaScript out of necessity. A good portion of the Internet runs on some form