Facebook Pixel

JavaScript: not defined vs ReferenceError vs TypeError

JS has three kinds of errors that confuse beginners: not defined, ReferenceError, and TypeError. Here is the difference.

JavaScript: not defined vs ReferenceError vs TypeError

JavaScript errors have confusing names. Here are the three most common ones and the difference between them.

"not defined" (a ReferenceError)

"not defined" is the message in a ReferenceError. It means the variable does not exist in any accessible scope.

console.log(foo); // ReferenceError: foo is not defined `` ### `ReferenceError` (the error type) A `ReferenceError` is thrown when the engine cannot resolve a variable reference. "not defined" and TDZ errors are both `ReferenceError`s: ```js console.log(x); // ReferenceError: x is not defined (never declared) console.log(y); let y; // ReferenceError: Cannot access 'y' before initialization (TDZ) `` ### `TypeError` A `TypeError` is thrown when a value is not the expected type. Common cases: - Calling a non-function: ```js const foo = undefined; foo(); // TypeError: foo is not a function `` - Reading a property of `null` or `undefined`: ```js const obj = null; obj.name; // TypeError: Cannot read properties of null (reading 'name') `` - Reassigning a `const`: ```js const x = 5; x = 10; // TypeError: Assignment to constant variable `` ### `SyntaxError` A `SyntaxError` is thrown during parsing, before any code runs. The entire script does not execute. ```js const x = ; // SyntaxError: Unexpected token ';' `` ### Comparison Table | Error | When | Example | |---|---|---| | `ReferenceError` | Variable does not exist or is in TDZ | `console.log(foo)` (never declared) | | `TypeError` | Value is wrong type for the operation | `undefined()` or `null.x` | | `SyntaxError` | Code is malformed | `const x = ;` | ### The Hoisting Connection ```js console.log(a); // undefined (var hoisted, no error) var a = 5; console.log(b); // ReferenceError (let in TDZ) let b = 5; bar(); // TypeError (var hoisted as undefined, calling undefined()) var bar = function () {}; foo(); // works (function declaration hoisted with body) function foo() {} `` ### The Takeaway `ReferenceError` ("not defined" or TDZ) means the variable does not exist or is not yet accessible. `TypeError` means the value is the wrong type for the operation (calling a non-function, reading properties of null/undefined). `SyntaxError` means the code is malformed and fails at parse time.

ReferenceError is thrown when a variable does not exist or is in the TDZ. TypeError is thrown when a value is the wrong type for the operation, like calling a non-function or reading a property of null or undefined.

Because the var is hoisted with undefined, so the variable exists (no ReferenceError). But undefined() throws TypeError: x is not a function, because the value is not callable.

An error thrown during parsing, before any code runs. Malformed code like const x = ; causes it. The entire script or module does not execute when there is a SyntaxError.

It is the message in a ReferenceError. It means the variable x does not exist in any accessible scope: it was never declared, is misspelled, is out of scope, or was not imported.

ReferenceError (variable does not exist or is in TDZ), TypeError (value is wrong type for the operation, like calling undefined or reading null.x), and SyntaxError (code is malformed and fails at parse time).

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.