# keyword

[JS](https://lochiwei.gitbook.io/web/js) ⟩ [grammar](https://lochiwei.gitbook.io/web/js/grammar) ⟩ [token](https://lochiwei.gitbook.io/web/js/grammar/token) ⟩ keyword

{% hint style="success" %}
A <mark style="color:purple;">**keyword**</mark> is a [](https://lochiwei.gitbook.io/web/js/grammar/token "mention") that matches [IdentifierName](https://262.ecma-international.org/13.0/#prod-IdentifierName) (looks like an [id](https://lochiwei.gitbook.io/web/js/grammar/token/id "mention")), but also has a <mark style="color:yellow;">**syntactic use**</mark>. (:blue\_book:[ECMA](https://262.ecma-international.org/13.0/#sec-keywords-and-reserved-words) )

```javascript
// 54 keywords
as async await break case catch class const continue 
debugger default delete do else enum export extends 
false finally for from function get 
if implements import in instanceof interface 
let meta new null of package private protected public return 
set static super switch target this throw true try typeof 
var void while with yield
```

{% endhint %}

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="success" %} <mark style="color:purple;">**keywords**</mark> can be divided into：

* [reserved](https://lochiwei.gitbook.io/web/js/grammar/token/keyword/reserved "mention")： \ <mark style="color:red;">**never allowed**</mark> as [id](https://lochiwei.gitbook.io/web/js/grammar/token/id "mention") (<mark style="color:orange;">**except**</mark> <mark style="color:blue;">`await`</mark>, <mark style="color:blue;">`yield`</mark>)
* [contextual](https://lochiwei.gitbook.io/web/js/grammar/token/keyword/contextual "mention")：
  * <mark style="color:orange;">**contextually**</mark>**&#x20;**<mark style="color:yellow;">**allowed**</mark> as [id](https://lochiwei.gitbook.io/web/js/grammar/token/id "mention")： \
    namely <mark style="color:blue;">`await`</mark>, <mark style="color:blue;">`yield`</mark>.
    * <mark style="color:blue;">`await`</mark> -  reserved only inside [functions](https://lochiwei.gitbook.io/web/js/async/async/functions "mention")s and [module](https://lochiwei.gitbook.io/web/js/module "mention")s.
  * <mark style="color:orange;">**contextually**</mark> <mark style="color:red;">**disallowed**</mark> as [id](https://lochiwei.gitbook.io/web/js/grammar/token/id "mention"), in [strict mode code](https://262.ecma-international.org/13.0/#sec-strict-mode-code):：\
    [let](https://lochiwei.gitbook.io/web/js/variable/declare/let "mention"), `static`, `implements`, `interface`, `package`, `private`, `protected`, `public`.
  * <mark style="color:yellow;">**mostly allowed**</mark> as [id](https://lochiwei.gitbook.io/web/js/grammar/token/id "mention"), but <mark style="color:orange;">**also appear as**</mark> [keyword](https://lochiwei.gitbook.io/web/js/grammar/token/keyword "mention")s <mark style="color:orange;">**within certain syntactic productions**</mark>, at places where [id](https://lochiwei.gitbook.io/web/js/grammar/token/id "mention") is <mark style="color:red;">**not allowed**</mark>: \
    `as`, `async`, `from`, `get`, `meta`, `of`, `set`, `target`.
    * <mark style="color:blue;">`async`</mark> -  can be used as a [variable](https://lochiwei.gitbook.io/web/js/variable "mention") <mark style="color:yellow;">**name**</mark> or [statement](https://lochiwei.gitbook.io/web/js/grammar/statement "mention") <mark style="color:yellow;">**label**</mark> without restriction.
      {% endhint %}

{% hint style="warning" %}
`NaN`/`Infinity`/ [undefined](https://lochiwei.gitbook.io/web/js/val/prim/undefined "mention") is：

* <mark style="color:red;">**not**</mark> [keyword](https://lochiwei.gitbook.io/web/js/grammar/token/keyword "mention").
* <mark style="color:yellow;">**immutable**</mark> & <mark style="color:yellow;">**read-only**</mark> [properties](https://lochiwei.gitbook.io/web/js/val/obj/prop) of the [object](https://lochiwei.gitbook.io/web/js/scope/global/object "mention").
* JS <mark style="color:red;">**doesn't throw**</mark> any errors if you declare them in the [global](https://lochiwei.gitbook.io/web/js/scope/global "mention").

:green\_book:[TeckTutorialHub](https://www.tektutorialshub.com/javascript/javascript-keywords-reserved-words/)
{% endhint %}

{% hint style="danger" %}

* <mark style="color:red;">**avoid**</mark> using [reserved](https://lochiwei.gitbook.io/web/js/grammar/token/keyword/reserved "mention") as [id](https://lochiwei.gitbook.io/web/js/grammar/token/id "mention")s (at least for [variable](https://lochiwei.gitbook.io/web/js/variable "mention")/[func](https://lochiwei.gitbook.io/web/js/val/func "mention")) :exclamation:
* in [dot-notation-.](https://lochiwei.gitbook.io/web/js/val/obj/prop/access/dot-notation-. "mention"), it's <mark style="color:yellow;">**OK**</mark> to use [reserved](https://lochiwei.gitbook.io/web/js/grammar/token/keyword/reserved "mention") as [name](https://lochiwei.gitbook.io/web/js/val/obj/prop/name "mention"):exclamation:
  {% endhint %}
  {% endtab %}

{% tab title="💈範例" %}

* replit：[reserved words](https://replit.com/@pegasusroe/reserved-words#index.js)
* require： [str.replacewhitespaces](https://lochiwei.gitbook.io/web/js/val/prim/str/method/str.replacewhitespaces "mention"), [arr.removeduplicates](https://lochiwei.gitbook.io/web/js/val/builtin/arr/ext/arr.removeduplicates "mention")

```javascript
// 54 keywords
as async await break case catch class const continue 
debugger default delete do else enum export extends 
false finally for from function get 
if implements import in instanceof interface 
let meta new null of package private protected public return 
set static super switch target this throw true try typeof 
var void while with yield
```

{% endtab %}

{% tab title="🔴 主題" %}

* [vs-keyword](https://lochiwei.gitbook.io/web/js/grammar/token/id/vs-keyword "mention")
* <mark style="color:yellow;">**types of keywords**</mark>
  * [reserved](https://lochiwei.gitbook.io/web/js/grammar/token/keyword/reserved "mention")
  * [contextual](https://lochiwei.gitbook.io/web/js/grammar/token/keyword/contextual "mention")
    {% endtab %}

{% tab title="📘 手冊" %}

* [Lexical grammar](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar) ⟩ [keywords](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#keywords)
* ECMA ⟩ [12.6.2 Keywords and Reserved Words](https://262.ecma-international.org/13.0/#sec-keywords-and-reserved-words) ⭐️
  {% endtab %}

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

* part of JavaScript's [..](https://lochiwei.gitbook.io/web/js/grammar "mention").
* [iskeyword](https://lochiwei.gitbook.io/web/js/val/prim/str/ext/iskeyword "mention") - check if string is a keyword.
  {% endtab %}

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

* [ ] TeckTutorialHub ⟩ [JavaScript Keywords & Reserved Words](https://www.tektutorialshub.com/javascript/javascript-keywords-reserved-words/) ⭐️
  {% endtab %}

{% tab title="🚧" %}

* [ ] undefined
* [ ] NaN
* [ ] Infinity
* [ ] variable name
* [ ] statement label
* [x] tokens
  {% endtab %}
  {% endtabs %}
