# not(f)

[JS](https://lochiwei.gitbook.io/web/js) ⟩ [value](https://lochiwei.gitbook.io/web/js/val) ⟩ [function](https://lochiwei.gitbook.io/web/js/val/func) ⟩ [higher-order](https://lochiwei.gitbook.io/web/js/val/func/kind/higher) ⟩ example ⟩ not(f) &#x20;

{% hint style="success" %}
logical negation of a function, that is：

```javascript
not(f)(...args) === !f(...args)
```

{% endhint %}

{% tabs %}
{% tab title="💾 程式" %}

* replit ⟩ [not(f)](https://replit.com/@pegasusroe/notf#index.js)

```javascript
// ⭐ logical negation of f
// - that is: not(f)(x) returns !f(x)
// - we can't write (!f)(x) directly, but now we can write not(f)(x).
function not(f) {
    return function(...args) {
        const result = f.apply(this, args);  // call f(...args) with `this` context
        return !result;                      // negate the result
    };
}
```

* example

```javascript
const isEven = x => x % 2 === 0;
const isOdd = not(isEven);

[1, 1, 3, 5, 5].every(isOdd)    // true
```

{% endtab %}

{% tab title="👥 相關" %}

* [boolean](https://lochiwei.gitbook.io/web/js/val/prim/boolean "mention")
  {% endtab %}

{% tab title="📗 參考" %}

* [ ] [javascript-the-definitive-guide](https://lochiwei.gitbook.io/web/master/ref/javascript-the-definitive-guide "mention") ⟩ 8.8.2 Higher-Order Functions ⭐️&#x20;
* [ ] Eloquent JavaScript ⟩ [higher-order functions](https://eloquentjavascript.net/05_higher_order.html#h_xxCc98lOBK)
  {% endtab %}
  {% endtabs %}
