SShortSingh.
Back to feed

JavaScript Hoisting and TDZ: Why var Returns undefined While let Throws Errors

0
·1 views

JavaScript engines process code in two phases: a creation phase that registers all variable and function declarations, and an execution phase that runs code line by line. Variables declared with var are registered and initialized to undefined during creation, so accessing them early silently returns undefined rather than throwing an error. In contrast, let, const, and class declarations are registered but left uninitialized, placing them in a Temporal Dead Zone (TDZ) until their declaration line is reached — any early access throws a ReferenceError. Common bugs arise when a var variable is used before its assigned branch executes, or when a function declaration calls a const that is still in the TDZ. Developers can prevent most hoisting-related runtime errors by enabling lint rules such as no-var and no-use-before-define, which catch these issues at write time.

Read the full story at DEV Community

This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)

Log in to join the discussion and vote.

Log in

Related stories

0
ProgrammingDEV Community ·

Developer proposes co-location pattern to fix recurring Sanity page builder bugs

A recurring bug in Sanity-based page builders causes sections to render blank or disappear silently when developers miss one of five required touch points during setup. The problem stems from section type definitions, GROQ projections, React components, renderer maps, and TypeScript types living in separate files that can fall out of sync. Developer Maciej Trzcinski argues that co-locating a section's type, query, and renderer into a single object declaration eliminates the most common failure modes. He has published two open-source packages implementing this pattern for Sanity Studio and Next.js frontends. The core idea, however, is framework-agnostic: treating the boilerplate dispatch logic as a managed dependency rather than hand-rolled code on every project.

0
ProgrammingDEV Community ·

Developer Shares Guide to Installing NixOS on Jetorbit VPS via nixos-anywhere

A developer documented the process of installing NixOS on a Jetorbit VPS using the nixos-anywhere tool, with all configurations managed from a macOS laptop running nix-darwin. The setup relies on SSH key-based root access and a one-time nixos-anywhere deployment, with subsequent updates handled via nixos-rebuild. Three critical pre-installation checks are highlighted: confirming the VPS uses legacy BIOS rather than UEFI, noting that Jetorbit assigns static IPs instead of DHCP, and ensuring the root SSH public key is registered on the server. The nixos-anywhere tool works by sending a small installer image to the running server and using kexec to boot into it without a full reinstall, then partitioning the disk via disko and building the NixOS system closure remotely. This approach eliminates configuration drift and keeps the server state fully reproducible from a git repository.

0
ProgrammingDEV Community ·

Why Complex Systems Fail Silently: Lessons from Bridges and AI-Assisted Code

A tech essay in the 'Craft & Code' series draws parallels between historic engineering failures and the hidden risks in modern software development. The author highlights two landmark cases: the Tacoma Narrows Bridge, which collapsed in 1940 due to unforeseen aerodynamic flaws, and the 1977 Citicorp Center in New York, which was quietly reinforced after its own engineer discovered a critical structural vulnerability post-construction. Unlike a crooked shelf, which reveals its flaw immediately, complex engineering can appear flawless while harboring serious defects beneath the surface. The piece argues that software, as one of the most complex forms of engineering, belongs in the same risk category as bridges and skyscrapers rather than simple craftsmanship. The author warns that as AI tools democratize software creation, the ability to detect invisible, potentially fatal flaws may erode before anyone notices it is gone.

0
ProgrammingDEV Community ·

Policy Alone Won't Stop AI Hallucinations in Law Firms, Infrastructure Will

In April 2026, Sullivan & Cromwell apologized to a federal bankruptcy judge after AI-generated hallucinations appeared in a court filing, despite the firm having stated safeguards in place. The incident highlights a broader distinction between policy-based compliance and technical infrastructure — policies set expectations but cannot intercept errors at the moment they are generated. Experts argue that law firms need an AI harness layer built into their workflows, including real-time citation verification, confidence-threshold routing, and ongoing model-drift monitoring. Without these technical controls, hallucinated content can pass through multiple human review stages undetected, especially under deadline pressure or when junior staff are involved. The Sullivan & Cromwell case is being cited as evidence that governed AI infrastructure, not just acceptable-use policies, must be treated as a core design requirement for legal work.

JavaScript Hoisting and TDZ: Why var Returns undefined While let Throws Errors · ShortSingh