The throw keyword[edit | edit source]
The throw keyword is used to be sure that an exception is caught when it occurs. It has the obligatory catch clause, and the optional finally clause. If the code inside the try block does not cause an exception, the code inside the finally block will execute, if it exists. If an error occurs, the comments inside the catch clause will execute, the finally block will execute, if it exists.
Examples[edit | edit source]
var result; try { result = log(-12.05); alert("Executed comment successfully."); } catch(err) { document.getElementById("demo").innerHTML = err.message; throw("An error occurred, and result could not be calculated!"); } finally { alert("result = " + result); // This line will execute anyway }
(An exception with the text "An error occurred, and result could not be calculated!" will be thrown.
result = undefinedSee also[edit | edit source]
- catch
- finally
- try
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
In JavaScript you cannot use these reserved words as variables names, labels names or function names.
Reserved Words:
abstract | arguments | boolean | break | byte |
case | catch | char | const | continue |
debugger | default | delete | do | double |
else | eval | false | final | finally |
float | for | function | goto | if |
implements | in | instanceof | int | interface |
long | native | new | null | package |
private | protected | public | return | short |
static | switch | synchronized | this | throw |
throws | transient | true | try | typeof |
var | void | volatile | while | with |
yield |
Reserved Words added in ECMAScript 5 and 6.
awaits | class | enum | export |
extends | import | let | super |
Removed Reserved Words
Following reserved words has been removed from ECMAScript 5 and 6.
abstract | boolean | byte | char |
double | final | float | goto |
int | long | native | short |
synchronized | throws | transient | volatile |
This section contains a list of all keywords that are reserved in JavaScript.
What are Reserved Keywords?
JavaScript has a number of reserved keywords. These are the words that you cannot use as identifiers (variable names, function names, and loop labels) in your JavaScript programs.
Reserved Keywords in ECMAScript 5 (ES5)
The following list shows the keywords that are reserved in ECMAScript 5. It also includes keywords that are reserved for future as well as keywords that are disallowed in strict mode.
arguments sm break case catch class fr const fr continue debugger default delete do else enum fr eval sm export fr extends fr false finally for function if implements sm import fr in instanceof interface sm let sm new null package sm private sm protected sm public sm return static sm super fr switch this throw true try typeof var void while with yield sm
Note: The keywords arguments and eval are not strictly reserved keywords, but they are treated like keywords in strict mode, so they cannot be used as identifiers. Additionally, the keywords null, true, and false cannot be used as identifiers in ECMAScript.
Reserved Keywords in ECMAScript 6 (ES6)
The following list shows the keywords that are reserved in ECMAScript 6. It also includes keywords that are reserved for future as well as keywords that are disallowed in strict mode.
arguments sm await break case catch class const continue debugger default delete do else enum fr eval sm export extends false finally for function if implements sm import in instanceof interface sm let sm new null package sm private sm protected sm public sm return static sm super switch this throw true try typeof var void while with yield
Future Reserved Keywords in Older Standards
The following list shows the keywords that were reserved as future keywords by the older ECMAScript specifications (before ECMAScript 5 or ES5).
abstract boolean byte char double final float goto int long native short synchronized throws transient volatile
Note: For optimal backwards compatibility, you should better avoid using all the keywords shown on this page as variable names or property names in your JavaScript code.