You might expect that calling a function before it is defined would cause an error. In JavaScript, it doesn't.
JavaScript tries to be "helpful" with Automatic Semicolon Insertion (ASI). But sometimes, it helps too much.
In many languages, if you forget to declare a variable, you get an error. In JavaScript (non-strict mode), you get a present : js the weird parts
Perhaps the most frustrating aspect of JavaScript for beginners is the behavior of the this keyword and the legacy of the var variable declaration. In languages like Java or C++, this usually refers to the current instance of the class. In JavaScript, this is dynamic; its value depends entirely on how a function is called, not where it is defined. This often leads to the infamous bug where a callback function loses its context, causing this to point to the global window object (or undefined in strict mode) instead of the intended object. Similarly, var suffers from "hoisting," where variable declarations are silently moved to the top of their scope, allowing code to run without error even if a variable is used before it is defined. While these features were designed to make the language forgiving, they often introduce silent errors that are difficult to debug.
: If a variable isn't in the current function, JS looks at the outer environment where the function was defined (Lexical Environment). You might expect that calling a function before
For developers, JavaScript is a language of paradoxes. It is the most widely used language in the world, yet it was famously prototyped in just . This rushed origin led to a collection of behaviors often called "The Weird Parts."
: Every object has a hidden property ( __proto__ ) pointing to another object. But sometimes, it helps too much
console.log(typeof NaN); // "number"