Blockless Scope: JavaScript Shenanigans
The article discusses the concept of blockless scope in JavaScript, particularly focusing on the differences between var and let. It explains how var is function-scoped while let is block-scoped, even in scenarios without explicit blocks. The author provides examples to illustrate these concepts and their implications in coding practices.
- ▪Variables declared with var are not local to the loop and persist after the loop completes.
- ▪Variables declared with let are local to the statement, providing a unique scope even without a block.
- ▪Lexical scoping determines variable availability based on their declaration location in the source code.
Opening excerpt (first ~120 words) tap to expand
try { if(localStorage) { let currentUser = localStorage.getItem('current_user'); if (currentUser) { currentUser = JSON.parse(currentUser); if (currentUser.id === 860505) { document.getElementById('article-show-container').classList.add('current-user-is-article-author'); } } } } catch (e) { console.error(e); } Samuel Rouse Posted on May 27 Blockless Scope: JavaScript Shenanigans #webdev #javascript #beginners #programming Shenanigans (3 Part Series) 1 🧐 JavaScript Shenanigans: Time Travel with Destructuring 2 Chaos Proxy: JavaScript Shenanigans 3 Blockless Scope: JavaScript Shenanigans As a JavaScript developer, you've likely been told that let and const are block scoped. But, how does that work when there is no block? Classic Example: var vs.
…
Excerpt limited to ~120 words for fair-use compliance. The full article is at DEV.to (Top).