> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/web/js/val/func/kind/higher/not-f.md).

# not(f)

[JS](/web/js.md) ⟩ [value](/web/js/val.md) ⟩ [function](/web/js/val/func.md) ⟩ [higher-order](/web/js/val/func/kind/higher.md) ⟩ 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](/web/js/val/prim/boolean.md)
  {% endtab %}

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

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