# Not keyword operators

**URL:** <https://es.discourse.group/t/not-keyword-operators/1097>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [November 17, 2021, 9:33pm UTC](https://es.discourse.group/t/not-keyword-operators/1097 "2021-11-17T21:33:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DiriectorDoc](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/diriectordoc/32/987_2.png) [@DiriectorDoc](https://es.discourse.group/u/DiriectorDoc)\
**Post date:** [November 17, 2021, 9:33pm UTC](https://es.discourse.group/t/not-keyword-operators/1097/1 "2021-11-17T21:33:40Z")

</div>

I'll make this quick.

What we do:

```javascript
// Keyword operators
Infinity instanceof Object // false
"length" in [] // true

// To check for not-instanceof or not-in
!(Infinity instanceof Object) // true
!("length" in []) // false

```

What I propose:

```javascript
Infinity !instanceof Object // true
"length" !in [] // false

```

In terms of readability, I'd say it's easier to follow. It also reduces the need for extra parentheses when doing `if(!(condition)){}`.

What do you think?

---

<div class="post-metadata">

**Author:** ![graphemecluster](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/graphemecluster/32/957_2.png) [@graphemecluster](https://es.discourse.group/u/graphemecluster)\
**Post date:** [November 26, 2021, 10:50am UTC](https://es.discourse.group/t/not-keyword-operators/1097/2 "2021-11-26T10:50:02Z")

</div>

Great idea, this syntax is possibly ambiguous with TypeScript’s non-null assertion operator though.

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [November 29, 2021, 11:23pm UTC](https://es.discourse.group/t/not-keyword-operators/1097/3 "2021-11-29T23:23:58Z")

</div>

I'm not sure this is quite common enough to merit new syntax for it.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [November 30, 2021, 2:28am UTC](https://es.discourse.group/t/not-keyword-operators/1097/4 "2021-11-30T02:28:24Z")

</div>

I write code like this all the time.

```javascript
try {
  ...
} catch (err) {
  if (!(err instanceof Whatever)) throw err
  ...
}

```

The `(!(...))` thing is pretty gross.

There are other languages who provide support for this sort of syntax, like Python.

```python
if x not in my_array:
  do_whatever()

```

However, I also understand that delegates are hoping to eventually improve JavaScript's catch syntax. When this happens, my use for instanceof will likely go way down. The "in" operator doesn't seem to be used a ton either, partially because many people treat a property explicitly set to undefined, and missing properties, as the same. The "in" operator will distinguish between these two scenarios, while syntax such as `if (obj.maybeMissingProperty != null)` does not. So, I can also see @claudiameadows's point.

---

<div class="post-metadata">

**Author:** ![kaizhu256](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/kaizhu256/32/342_2.png) [@kaizhu256](https://es.discourse.group/u/kaizhu256)\
**Post date:** [November 30, 2021, 6:01am UTC](https://es.discourse.group/t/not-keyword-operators/1097/5 "2021-11-30T06:01:06Z")

</div>

going off-topic, but your `try {...} catch (e) { if (...) throw e }` design-pattern is PITA to write test-coverage for.

a more coverage-friendly design-pattern is with helper `assertOrThrow()` -- a reusable-function whose only ux-purpose is boosting code-coverage for above-stated and similar error-handling scenarios.

```javascript
+function assertOrThrow(condition, message) {
+ // This function will throw <message> if <condition> is falsy.
+ if (!condition) {
+ throw (
+ (!message || typeof message === "string")
+ // if message is string, convert to Error and throw
+ ? new Error(message)
+ // if message is object, assume error-object and throw directly
+ : message
+ );
+ }
+}
 
 try {
   ...
 } catch (err) {
- if (!(err instanceof Whatever)) throw err
+ // use helper-function to avoid writing tedious code-coverage
+ // for the if-branch
+ assertOrThrow(err instanceof Whatever, err);
   ...
 }

```

---

<div class="post-metadata">

**Author:** ![graphemecluster](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/graphemecluster/32/957_2.png) [@graphemecluster](https://es.discourse.group/u/graphemecluster)\
**Post date:** [December 4, 2021, 4:42pm UTC](https://es.discourse.group/t/not-keyword-operators/1097/6 "2021-12-04T16:42:37Z")

</div>

Will there be any advantage if we introduce a new keyword like Swift’s `guard` (which is similar to “if not”)?

---

<div class="post-metadata">

**Author:** ![lightmare](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lightmare/32/843_2.png) [@lightmare](https://es.discourse.group/u/lightmare)\
**Post date:** [December 4, 2021, 7:50pm UTC](https://es.discourse.group/t/not-keyword-operators/1097/7 "2021-12-04T19:50:02Z")

</div>

That sounds like reinventing Perl's `unless` ;) Having such statement won't help in expressions.

---

<div class="post-metadata">

**Author:** ![graphemecluster](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/graphemecluster/32/957_2.png) [@graphemecluster](https://es.discourse.group/u/graphemecluster)\
**Post date:** [December 5, 2021, 3:43am UTC](https://es.discourse.group/t/not-keyword-operators/1097/8 "2021-12-05T03:43:50Z")

</div>

Well, I guess so. Didn’t think of the word “unless” :(
