Monday, February 3, 2020

Reserved keywords in Python, Javascript and R

These are a set of words that have special meaning and cannot be used as an identifier (that is as variable name, function name, class name etc.). These are the building block of Python, Javascript and R programming languages

Avoid using these reserved words and keywords as function or variable names as Python, JavaScript and R has reserved these words for their own use.


List of reserved keywords in Python

['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']





List of reserved keywords in JavaScript
abstractargumentsawait*boolean
breakbytecasecatch
charclass*constcontinue
debuggerdefaultdeletedo
doubleelseenum*eval
export*extends*falsefinal
finallyfloatforfunction
gotoifimplementsimport*
ininstanceofintinterface
let*longnativenew
nullpackageprivateprotected
publicreturnshortstatic
super*switchsynchronizedthis
throwthrowstransienttrue
trytypeofvarvoid
volatilewhilewithyield
Words marked with* are new in ECMAScript 5 and 6. (Source: https://www.w3schools.com/js/js_reserved.asp)


List of reserved keywords in R

if, else, repeat, while, function, for, in, next, break, TRUE, FALSE, NULL, Inf, NaN, NA, NA_integer_, NA_real_, NA_complex_, NA_character_





Checking for reserved keywords

The above keywords may get altered in different versions of Python, JavaScript and R. Some extra might get added or some might be removed. So, how do you look-up for these reserved words in either Python, JavaScript or R?

You can always get the list of keywords in your current version as follow:-


Checking for reserved keywords in Python

import keyword
print(keyword.kwlist)
In python interpreter, run the above lines to view the list.



Checking for reserved keywords in JavaScript

Unlike in python and R, I am not sure javascript has a way to call a built-in function to display its available reserved keywords. So, you need to check a reliable archive such as the Mozilla Developer Doc for up to date list of reserved keywords in javascript.



Checking for reserved keywords in R

To check for the keywords, type the following at the R command prompt as follows.

help(reserved)
or

?reserved

Thank you for reading.

No comments:

Post a Comment