mirror of
https://github.com/jbranchaud/til
synced 2026-01-06 00:28:01 +00:00
Add Obtain Undefined Value With The Void Operator as a javascript til
This commit is contained in:
24
javascript/obtain-undefined-value-with-the-void-operator.md
Normal file
24
javascript/obtain-undefined-value-with-the-void-operator.md
Normal file
@@ -0,0 +1,24 @@
|
||||
# Obtain Undefined Value With The Void Operator
|
||||
|
||||
The [`void`
|
||||
operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void)
|
||||
takes any expression, evaluates it, and then results in the `undefined` value.
|
||||
A common use of the `void` operator is to get the primitive `undefined` value
|
||||
in a consistent way.
|
||||
|
||||
```javascript
|
||||
> void(0);
|
||||
undefined
|
||||
```
|
||||
|
||||
This is handy for instances where you need to check if a value is `undefined`:
|
||||
|
||||
```javascript
|
||||
function doSomething({ arg }) {
|
||||
if (arg === void 0) {
|
||||
throw new Error("arg is undefined 😱");
|
||||
}
|
||||
|
||||
// ...
|
||||
};
|
||||
```
|
||||
Reference in New Issue
Block a user