1. Variables: What They Are and How to Name Them
Variables are like containers that can hold information. We use variables to store the results of calculations, the values entered by users, or any other data that we need to use in our program. Variables are important because they allow us to write more complex and more reusable programs.
JavaScript variable names can be any sequence of letters, numbers, underscores, and dollar signs, but they must not start with a number. Variable names must also be unique and cannot be the same as any of JavaScript's reserved words.
Here are some examples of valid JavaScript variable names:
my_variable
heightInPixels
sumOfTwoNumbers
Here are some examples of invalid JavaScript variable names:
1my_variable (cannot start with a number)
function (reserved word)
It is important to choose descriptive names for your variables, so that your code is easier to read and understand. For example, instead of naming a variable x, you could name it height or width.
JavaScript has a list of reserved words that cannot be used as variable names. These words have special meanings in the language and cannot be used for other purposes. For example:
abstract | arguments | await | boolean |
break | byte | case | catch |
char | class | const | continue |
debugger | default | delete | do |
double | else | enum | eval |
export | extends | false | final |
finally | float | for | function |
goto | implements | if | import |
in | instanceof | int | interface |
let | long | native | new |
null | package | private | protected |
public | return | short | static |
super | switch | synchronized | this |
throw | throws | transient | true |
try | typeof | var | void |
volatile | while | with | yield |