Avoid Common JavaScript Hoisting Issues By Mastering Variables

Avoid Common JavaScript Hoisting Issues By Mastering Variables

Master variables in JavaScript and avoid Hoisting with var, let and const

Preamble

The type of a variable is being set by the value it is holding. This is the origin of this dynamic nature of the type system inside JavaScript. In C#, for example, we got strongly typed variables where it behaves the other way around. The type is assigned to the variable. Therefore, C## knows a declared but not initialized variable, whereas JavaScript has to return an undefined as a universal answer instead of null.

But what has this in common with const, let, and var?

Hoisting

To answer this question, we should look into the ways of declaring a variable in JavaScript. We got only three options to do so.

  1. Var
  2. Let
  3. Const

Var is the true source of them all because let and const came in with ES2015. Var is a pretty common keyword, even today when we got let and const. It is better not to use it, though. Because a variable defined with var is just like a local variable in C#. It is only usable inside this function, that declared it.

What is so different here? I provided two examples for you. The first one declares the variable in the middle of the method and initializes it right away.

coding programming javascript nodejs

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Principles of Functional Programming in JavaScript That Will Make Your Coding Life Easier

Who else loves to write side-effects-free functions? I think we, as programmers, all do. Today, in this story, I will walk you through the basic principles of functional programming that will make your coding life easier.

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript. Bugs are born in many ways. Creating side effects is one of them. Some people say side effects are evil, some say they’re not.

Who Else Wants to Write Clean JavaScript Code?

Who Else Wants to Write Clean JavaScript Code? 7 Tips to Make Your Coworkers Fall in Love With Your Code.

Code Formatting with Prettier in Visual Studio Code

Formatting code consistently is a pain, especially when working on a team. The beauty of modern-day web development is that the tooling has gotten so much better! In this article, we will look at setting up Prettier to automatically format your code in Visual Studio Code.

Top 15 Simple Coding Techniques to Get Your Tasks Done with Shorter Code in JavaScript

Don’t waste time writing long code while you can make it short, yet clearer and easier to read. In this Javascript tutorial, we'll discuss 15 Simple Coding Techniques to Get Your Tasks Done with Shorter Code in JavaScript