It is customary to declare variables using the var keyword, i.e. each new variable must be written on a separate line: var cat;
cat = Vera;
Optionally, you can assign a value to a variable immediately:
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:
x = 2;
x += 5; (it means to store the value in the variable
x + 5)
Dynamic typing of variables:
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?
To find out the type of a variable, use the typeof operator:
var cat = “vera”;
NaN(result, Not a Number)
With the help of the isNaN function, we can check if NaN did not get out somewhere when operating with different types of variables.