- Non-primitive (objects)
The principal data types are
. 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
can be one of the following—"
", or "
var n = 1;
|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.|
are also objects.
Number data type
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
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;
before a hexadecimal value (also called hex for short).
var n4 = 0x00;
var n5 = 0xff;
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.
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.
Dividing by 0 will give you infinity.
var a = 6 / 0;
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;
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
-Infinity + Infinity
Any other arithmetic operation with Infinity as one of the operands will give you Infinity:
Infinity - 20
-Infinity * 3
Infinity / 2
Infinity - 99999999999999999
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.
var 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";
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
String data type
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: