VOOZH about

URL: https://www.digitalocean.com/community/tutorials/understanding-variables-scope-hoisting-in-javascript?comment=89716

โ‡ฑ Understanding Variables, Scope, and Hoisting in JavaScript | DigitalOcean


Understanding Variables, Scope, and Hoisting in JavaScript

Updated on August 24, 2021
๐Ÿ‘ Understanding Variables, Scope, and Hoisting in JavaScript

Introduction

Variables are a fundamental part of many programming languages, and are among the first and most important concepts for novice coders to learn. There are a number of different properties of variables in JavaScript, as well as several rules which must be followed when naming them. In JavaScript, there are three keywords used to declare a variable โ€” var, let, and const โ€” and each one affects how the code will interpret the variable differently.

This tutorial will cover what variables are, how to declare and name them, and also take a closer look at the difference between var, let, and const. We will also review the effects of hoisting and the significance of global and local scope to a variableโ€™s behavior.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

Tutorial Series: How To Code in JavaScript

JavaScript is a high-level, object-based, dynamic scripting language popular as a tool for making webpages interactive.

About the author(s)

Software engineer and open source creator

๐Ÿ‘ Mark Drake
Mark Drake
Editor
Manager, Developer Education
See author profile

Former Technical Writer at DigitalOcean. Focused on SysAdmin topics including Debian 11, Ubuntu 22.04, Ubuntu 20.04, Databases, SQL and PostgreSQL.

Still looking for an answer?

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

will you be so kind, to add any additional articles and tutorials, please)

It is not easy at all to use it

Hi Tania, Mark. Thanks for the post. One small typo in the hoisting section; You say โ€œThis behavior can cause issues, though, because the programmer who wrote this code likely expects the output of x to be true, when it is instead undefined.โ€ but I think you mean โ€œThis behavior can cause issues, though, because the programmer who wrote this code likely expects the output of x to be 100, when it is instead undefined.โ€

Nice article, but i think you should add in this article the โ€œtemporal dead zoneโ€ for const and let declarations. Cheers

Wow! Thanks, I got something new to learn. I found good post on this topic which is http://www.visionfortech.com/2018/08/let-vs-var-in-javascript.html So i thought let me share that post with all of you it is well written and well explained by its author.

This was a great resource Thanks for the manager of this site good lunch

Thanks so much, well done! Running the code is what drives home the learning for meโ€ฆ

All declarations (function, var, let, const and class) are hoisted in JavaScript, while the var declarations are initialized with undefined , but let and const declarations remain uninitialized.

๐Ÿ‘ Creative Commons
This work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
  • Deploy on DigitalOcean

    Click below to sign up for DigitalOcean's virtual machines, Databases, and AIML products.

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and AI-native businesses

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOceanโ€™s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow โ€” whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

ยฉ 2026 DigitalOcean, LLC.Sitemap.
Dark mode is coming soon.