The principal data types are
Boolean. The other two data types are
To know the data type of a variable or a value, you can use the special typeof operator. This operator returns a string that represents the data type. The return values of using
typeof can be one of the following—"
object", or "
var n = 1; typeof n; "number"
|This includes floating point numbers as well as integers, for example 1, 100, 3.14. [number data type can store positive and negative integers or floats, hexadecimal numbers, octal numbers, exponents, and the special numbers NaN, Infinity, and –Infinity]|
|Any number of characters, for example "a", "one", "one 2 three". [The string data type contains characters in quotes]|
|Can be either true or false.[ Represents the two logical values true and false]|
|The undefined keyword indicates that the type of a variable is undefined as long as you have not initialized it with a value.|
Boolean are also objects.
The Number data type represents positive or negative numbers and positive or negative floating point numbers with or without exponents.
A floating-point number is compounded of an integer part that is followed by a decimal point, a decimal part and the letter
E followed by an exponent for scientific notation.
You can write an integer in decimal format(base 10), in hexadecimal format (base 16) or in octal format (base 8)
|Decimal||The decimal notation system corresponds to all classical numbers. In order to represent a negative number prefix with minus sign(-)|
|Hexadecimal||The hexadecimal notation system is based on 16 digits that are coded from 0 to 9 and then from A to F(A equal to 10 and F equal to 15. (To represent integer in hexadecimal notation in literal format you must prefix the number by 0x or 0X.|
|Octal||The octal notation systems is based on 8 figures that atre coded from 0 to 7. In the base 8 the eight digit is equal to 10. To represent an inter=ger in octal notation in literal format you must prefix the number by 0.|
In addition there are certain specific number values.
|NaN||Represents Not a Number|
When a number starts with a 0, it's considered an octal number. For example, the octal 0377 is the decimal 255.
var n3 = 0377; typeof n3; "number" n3; 255
0x before a hexadecimal value (also called hex for short).
var n4 = 0x00; typeof n4; "number" n4; 0 var n5 = 0xff; typeof n5; "number" n5; 255
1e1 (can also be written as 1e+1 or 1E1 or 1E+1) represents the number one with one zero after it, or in other words 10. Similarly, 2e+3 means the number 2 with 3 zeros after it, or 2000.
1e1 10 1e+1 10 2e+3 2000 typeof 2e+3; "number"
2e+3 means moving the decimal point 3 digits to the right of the number 2. There's also 2e-3 meaning you move the decimal point 3 digits to the left of the number 2.
2e+3 2e188.8.131.52.0.0.0.2.20000.002123321 2e-3 0.002 123.456E-3 0.123456 typeof 2e-3 "number"
Infinity Infinity typeof Infinity "number" 1e309 Infinity 1e308 1e+308
Dividing by 0 will give you infinity.
var a = 6 / 0; a
Infinity is the biggest number (or rather a little bigger than the biggest), but how about the smallest? It's infinity with a minus sign in front of it, minus infinity.
var i = -Infinity; i -Infinity typeof i "number"
This doesn't mean that the value to be exactly twice as big as Infinity—from 0 up to infinity and then from 0 down to minus infinity. Because there's no practical value to it. When you sum infinity and minus infinity, you don't get 0, but something that is called NaN (Not A Number).
Infinity - Infinity NaN -Infinity + Infinity NaN
Any other arithmetic operation with Infinity as one of the operands will give you Infinity:
Infinity - 20 Infinity -Infinity * 3 -Infinity Infinity / 2 Infinity Infinity - 99999999999999999 Infinity NaN
Nan - "Not A Number", It is a special value that is also a number. You get NaN when you try to perform an operation that assumes numbers but the operation fails.
typeof NaN "number" var a = NaN; a NaN
For example, if you try to multiply 10 by the character "f", the result is NaN, because "f" is obviously not a valid operand for a multiplication.
var a = 10 * "f"; a NaN
NaN is contagious, so if you have even only one NaN in your arithmetic operation, the whole result goes down the drain.
1 + 2 + NaN NaN
In addition you can include literal characters in your strings. These characters are also called control characters. They are not printable characters, but they allow you to represent special characters.
All values become true when converted to a boolean, with the exception of the six falsy values:
"" null undefined 0 NaN false