Variables in JavaScript

t is theoretically possible to program without them, in practice they are indispensable. It’s about variables. Here’s a brief (but not short) up-to-date summary of the most important things about variables in JavaScript, including examples and considering constants.

Rules for writing variables
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: currentElementPositionXor abbreviat curElPosX). In the case of constants, the same rules apply, but a different convention – it is customary to use philippines phone number data only capital 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 strategic use of social meia 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 will find that it is essentially a variation of constantly bz lists repeating constructions, and it is the names that give them meaning, explainwhy choose apt names .

 

Scroll to Top