Home » Variables in JavaScript

Variables in JavaScript

A variable name must begin with a letter, a dollar ($), or an underscore (_). It must not start with a number, which is sometimes a shame, but on the other hand, this restriction makes it easier for computers to distinguish between numbers and identifiers (ie names of variables, functions, etc.). As for letters, it is allow to use Czech characters (letters with diacritics), but it is recommend, and it is a common practice, to use only letters of the English alphabet. Although it complicates the writing of Czech variable names (for example, there is quite a difference between width and width ), but at least there is no risk of bad processing.

The rest of the variable name can already

Contain numbers. However, it definitely does not include the hyphen (-) or any other characters that are interpret as operators in JavaScript. And of course, the name of the variable must not be any of the words that are part of the JavaScript syntax, i.e. keywords (

break, do, in, typeof, case, else, instanceof, var, catch, export, new, void, class, extends, return, while, const, finally, super, with, continue, for, switch, yield, debugger, function, this, default, if, throw, delete, import, try, await)

, the values ​​of true, false, null. In addition to these, there are keywords reserv for consideration in other versions of JavaScript enum, implements, interface, package, private, protect,public.

The length of the variable name is arbitrary, so it is possible to name it with only one letter. In most cases, however, it is better to choose longer names that well describe the meaning of the given variable, even multi-word names. Spaces are not allow in the variable name, but you can help yourself in other ways, for example starting words with capital letters or using an underscore as a separator. It is best to stick to the establish convention of capitalizing every other word in a variable example.

Element PositionXor abbreviat curElPos

In the case of constants, the same rules apply, but a different convention – it is customary to use only capital  indonesia phone number data letters and underscores. You ne to be careful about upper and lower case letters, because JavaScript is case sensitive.

phone number data

It is worth thinking about the naming to the main thing is of variables for a while, because an imprecise or vague variable name will not tell anyone anything when iting the code later. Choosing the right name is very important because, if you look carefully at your code, you cz lists will find that it is essentially a variation of constantly repeating constructions, and it is the names that give them meaning, explains why choose apt names.

 

Scroll to Top