JavaScript – good manners


In JavaScript, it is customary to name variables in small letters (if the name is one word) – cat; If the variable name consists of two words, then camel case notation is used, i.e. the first word is written in small letters, and the second is glued to the first and written with a capital letter: myCat; If there are several words, then the first one is written with a small letter, and each new word is glued with the previous ones and is written with a capital letter: myDearCat;

It is customary to declare variables using the var keyword, i.e. each new variable must be written on a separate line: var cat;

In JavaScript, dynamic typing of variables, i.e. I set a variable via var, but it is not necessary to assign a value to it immediately and set the type of the variable (number/boolean/string):

varcat;
cat = Vera;

Optionally, you can assign a value to a variable immediately:

varcat=Vera;

Several variables can be explained at once:

vat cat, dog, kitten;

You can assign values to different variables at once:

var cat=kitten, dog, x=2;

The symbol equals = is in this case the variable assignment operator.

If we took a variable, performed an operation with it and want to save the new value to this variable:

var x;
x = 2;
x += 5; (it means to store the value in the variable x value x + 5)

Dynamic typing of variables:

var cat=10;
cat=kitten;

You can assign any values to variables, even numbers, even lines of text, and change this as the code progresses.

How to translate one type of variables into another?

Converting a string to a number – through multiplication by one:

‘9’*1 get 9

Casting a string to a number – through + in front of the string:

+’9′ we get 9

Casting a string to an integer:

parseInt(‘9,6’) get 9

Converting a string to a number with a comma:

parseFloat(‘9,6’) get 9.6

Converting anything to a number:

Number(‘9’) get 9
Number(false) we get 0
Number(‘false’) get NaN

Casting anything to a string:

9+”; get ‘9’
string(9); get ‘9’

Casting anything to boolean type:

Boolean(9) get true
!!9 we get true (this works because ! is a logical operator, therefore, !9 is interpreted first and at the same time 9 is converted to boolean type -> !true is fasle -> then the second one is executed! -> !false is true)

How to control your code, preventing errors?

The JavaScript programming language does not have a compiler that will throw an error if you screwed up somewhere. The interpreter here is very soft, it can add and glue almost anything and give the result.

To find out the type of a variable, use the typeof operator:

var cat = “vera”;
typeofcat;
“string” (result)

isNaN() function

‘cat’*10;
NaN(result, Not a Number)

isNaN(‘cat’*10);
true (answer)

With the help of the isNaN function, we can check if NaN did not get out somewhere when operating with different types of variables.

isFinite() function

isFinite(9)
true (answer)

*In JavaScript, you can divide by zero, so we can easily get infinity: 9/0 = Infinity

 

isFinite(9/0)
false (answer)


This entry was posted in Javascript (en). Bookmark the permalink.

Leave a Reply

🇬🇧 Attention! Comments with URLs/email are not allowed.
🇷🇺 Комментарии со ссылками/email удаляются автоматически.