Lecture
There are several basic data types in JavaScript.
number
:
var n = 123; |
n = 12.345; |
There are special numerical values Infinity
(infinity) and NaN
(computation error). They also belong to the type "number".
For example, Infinity
infinity is obtained by dividing by zero:
1 |
alert( 1 / 0 ); // Infinity |
The NaN
calculation error will be the result of an incorrect mathematical operation, for example:
1 |
alert( "нечисло" * 2 ); // NaN, ошибка |
var str = "Мама мыла раму" ; |
str = 'Одинарные кавычки тоже подойдут' ; |
Some programming languages have a special data type for one character. For example, in C, this is char
. In JavaScript, there is only the type "string" string
. Which, I must say, is quite convenient ..
boolean
. It has only two values - true
(true) and false
(false).
Typically, this type is used to store a yes / no value, for example:
var checked = true ; // поле формы помечено галочкой |
checked = false ; // поле формы не содержит галочки |
null
is a special value. It makes sense of “nothing.” The null
value does not belong to any of the types above, but forms its own separate type consisting of a single null
value:
var age = null ; |
null
not a “reference to a non-existent object” or “null pointer”, as in some other languages. This is just a special meaning that makes sense of “nothing” or “value unknown”.
In particular, the code above says that age
unknown.
undefined
is a special value that, like null
, forms its own type. It makes sense "not assigned".
If a variable is declared, but nothing is written to it, then its value is exactly undefined
:
1 |
var u; |
2 |
alert(u); // выведет "undefined" |
undefined
and explicitly, although this is rarely done:
var x = 123; |
x = undefined ; |
In an explicit form, undefined
usually not assigned, since it contradicts its meaning. To write to the "empty value" variable, use null
.
The first 5 types are called "primitive" .
The sixth type stands apart: “objects” . It includes, for example, dates, it is used for data collections and for many other things. Later we will return to this type and consider its fundamental differences from primitives.
Comments
To leave a comment
Scripting client side JavaScript, jqvery, BackBone
Terms: Scripting client side JavaScript, jqvery, BackBone