Skip to content

Commit 0716a08

Browse files
contents: fix behavior for typeof for undeclared variables (#11)
Co-authored-by: Yangshun Tay <tay.yang.shun@gmail.com>
1 parent 022d8c4 commit 0716a08

File tree

1 file changed

+8
-2
lines changed
  • questions/whats-the-difference-between-a-variable-that-is-null-undefined-or-undeclared-how-would-you-go-about-checking-for-any-of-these-states

1 file changed

+8
-2
lines changed

questions/whats-the-difference-between-a-variable-that-is-null-undefined-or-undeclared-how-would-you-go-about-checking-for-any-of-these-states/en-US.mdx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ subtitle: How would you go about checking for any of these states?"
88
| Trait | `null` | `undefined` | Undeclared |
99
| --- | --- | --- | --- |
1010
| Meaning | Explicitly set by the developer to indicate that a variable has no value | Variable has been declared but not assigned a value | Variable has not been declared at all |
11-
| Type | `object` | `undefined` | Throws a `ReferenceError` |
11+
| Type (via `typeof` operator) | `'object'` | `'undefined'` | `'undefined'` |
1212
| Equality Comparison | `null == undefined` is `true` | `undefined == null` is `true` | Throws a `ReferenceError` |
1313

1414
---
1515

1616
## Undeclared
1717

18-
**Undeclared** variables are created when you assign a value to an identifier that is not previously created using `var`, `let` or `const`. Undeclared variables will be defined globally, outside of the current scope. In strict mode, a `ReferenceError` will be thrown when you try to assign to an undeclared variable. Undeclared variables are bad just like how global variables are bad. Avoid them at all cost! To check for them, wrap its usage in a `try`/`catch` block.
18+
**Undeclared** variables are created when you assign a value to an identifier that is not previously created using `var`, `let` or `const`. Undeclared variables will be defined globally, outside of the current scope. In strict mode, a `ReferenceError` will be thrown when you try to assign to an undeclared variable. Undeclared variables are bad in the same way that global variables are bad. Avoid them at all cost! To check for them, wrap its usage in a `try`/`catch` block.
1919

2020
```js
2121
function foo() {
@@ -26,6 +26,12 @@ foo();
2626
console.log(x); // 1
2727
```
2828

29+
Using the `typeof` operator on undeclared variables will give `'undefined'`.
30+
31+
```js
32+
console.log(typeof y === 'undefined'); // true
33+
```
34+
2935
## `undefined`
3036

3137
A variable that is `undefined` is a variable that has been declared, but not assigned a value. It is of type `undefined`. If a function does not return any value as the result of executing it is assigned to a variable, the variable also has the value of `undefined`. To check for it, compare using the strict equality (`===`) operator or `typeof` which will give the `'undefined'` string. Note that you should not be using the loose equality operator (`==`) to check, as it will also return `true` if the value is `null`.

0 commit comments

Comments
 (0)