Data Types

Javascript has two data types.

  • Primitive
  • Non-primitive (objects)


The principal data types are Number, String, Object and Boolean. The other two data types are null and undefined.

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—"number", "string", "boolean", "undefined", "object", or "function".
var n = 1;
typeof n;
"number"


DataTypeDescription
NumberThis 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]
StringAny number of characters, for example "a", "one", "one 2 three". [The string data type contains characters in quotes]
Objectjavascript objects depend primarily on the Object type. This type defines a set of characteristics and common properties for all the Javascript objects.
BooleanCan be either true or false.[ Represents the two logical values true and false]
NullWhen you try to access a variable that doesn't exist, you get the special value undefined. The same will happen when you have declared a variable, but not given it a value yet. JavaScript will initialize it behind the scenes, with the value undefined.[The null value means "no value"]
UndefinedThe undefined keyword indicates that the type of a variable is undefined as long as you have not initialized it with a value.


Number, String, Object and Boolean 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 e or 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)

Base TypeDescription
DecimalThe decimal notation system corresponds to all classical numbers. In order to represent a negative number prefix with minus sign(-)
HexadecimalThe 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.
OctalThe 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.

ValueDescription
NaNRepresents Not a Number
MAX_VALUERepresents the largest number in Javascript
MIN_VALUERepresents the smallest positive number in Javascript
NEGATIVE_INFINITYRepresents a number that is smaller than the most negative number in javascript
POSITIVE_INFINITYRepresents a number that is larger than the largest number in javascript.


Octal Numbers
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

Hexadecimal Numbers
In JavaScript, you put 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

Exponent Literals
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
2e32.0.0.0.0.0.0.2.20000.002123321
2e-3
0.002
123.456E-3
0.123456
typeof 2e-3
"number"

Infinity
..........
There is a special value in JavaScript called Infinity. It represents a number too big for JavaScript to handle. Infinity is indeed a number, as typing typeof Infinity in the console will confirm. You can also quickly check that a number with 308 zeros is ok, but 309 zeros is too much. To be precise, the biggest number JavaScript can handle is 1.7976931348623157e+308 while the smallest is 5e-324.

Infinity
Infinity
typeof Infinity
"number"
1e309
Infinity
1e308
1e+308
Dividing by 0 will give you infinity.
var a = 6 / 0;
a
Infinity
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
....
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

String data type
With javascript, you delimit character strings with single or double quotation marks. You must use single-quotation marks for strings that contain double quotation marks and conversely you must use double quotation marks for string that contain single quotation .

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.

Control characterMeaning
\\backslash
\'single quote
\"double quote
\bback space
\rcarriage return
\ttab
\fpage break
\nnew line


All values become true when converted to a boolean, with the exception of the six falsy values:
""
null
undefined
0
NaN
false
Related Tutorial
6 Switch
7 Loops
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 # # # # #