There are words used in JavaScript that are intended only for very specific purposes. These keywords are needed to allow for JavaScript to have built-in functionality and standardized syntax
An example would be if. When writing in English, using if has an expected outcome: "Dinner will be cold if you wait too long". This sentence handles a possible situation and the results that would stem from it. Using if differently, like as a verb in the the place of "wait", would stop the sentence from working properly: "Dinner will be cold if you if too long". Likewise, in JavaScript, the purpose is similar in that a possible situation is checked and the results trigger a specific action: if(2 === 2) {...the resulting code}
.
Given the above, these reserved keywords can not be used as variables or in any way other than intended. When the browser is interpreting the JavaScript code it will act on these keywords as defined in the JavaScript spec, regardless of what the intentions were of the code. Like the example above where if is misused to mean "wait", similar misuse of a reserved keyword in JavaScript can produce unexpected results, be confusing for future programmers and create unstable code.
Here is a list of all currently reserved Keywords
- do
- if
- in
- for
- let
- new
- try
- var
- case
- else
- enum
- eval
- null
- this
- true
- void
- with
- await
- break
- catch
- class
- const
- false
- super
- throw
- while
- yield
- delete
- export
- import
- public
- return
- static
- switch
- typeof
- default
- extends
- finally
- package
- private
- continue
- debugger
- function
- arguments
- interface
- protected
- implements
- instanceof
Below is a link to a the JS keyword list including other related programming languages that JavaScript might interact with. It can be good practice to respect these keywords, as well.
Reserved Words in Other Languages →
The following link is to a great article by Mathias Bynens that shows the keyword set for each release of Javascript.