In ECMAScript, there are 5 basic types. Undefined, Null, Boolean,Number,String. And another type is a little bit complex which is Object.
The typeof
typeof is used to check the variable’s type. There are several possible return values.
undefined , if the variable is not defined.
boolean, if the variable’s value is boolean.
string , if the variable’s value is string.
number, if the value is number.
object, if the value is an object or null
function, if the variable is function.
1 | var message = 'some string'; |
Note: if you run typeof null , the return value is object.
Undefined
Undefined type only has one value which is undefined. When you use var to declare a variable but no value is assigned to that variable. This variable’s value now is undefined.
1 | var message; |
1 | var message; |
but here is another example.
1 | var message; |
When you run typeof on undeclared or uninitialized variable, the return value is undefined.
Null
1 | alert(null == undefined); // true; |
Boolean
Boolean has two values: true and false. But in ECMASript, all type’s value could be converted to Boolean.
1 | var message = 'Hello world'; |
| Type | Converted to true | Converted to false |
|---|---|---|
| Boolean | true | false |
| String | any not-empty string | empty string “” |
| Number | any number not 0 | 0 and NaN |
| Object | any object | null |
| Undefined | undefined |
Number
1 | var octalNum1 = 070; |
Number range
We could get max and min number by
1 | Number.MIN_VALUE; |
Any number is greater or less than max or min value will be converted to
1 | Infinity; |
isFinite() could be used to check if the nubmer is finite or not
1 | var result = Number.MAX_VALUE + Number.MAX_VALUE; |
NaN
NaN does not equal to any value includes itself.
1 | alert(NaN == NaN); //false; |
isNaN() could be used to check if the value could be converted to a number or not;
when it receives a value, it will try wo convert it to number.
1 | alert(isNaN(NaN)); //true; |
when an object is passed as an argument to isNaN, object’s valueOf() will be invoked and then toString() will be invoked if valueOf()’s return value could not be converted number.
Convert to number
Number()
Rules:
- if Boolean, true and false will be convertd to 1 and 0;
- if number, just simple pass and return;
- if
null, return 0; - if
undefined, returnNaN - if
string- if the string only contains number, will be converted to decimal digits. 0 will be ignored if the string is started with 0.
011will be converted to11,00000001will be converted to1 - same for float number
- if the string is a valid hexadecimal digits, it will be converted to decimal digits.
- if empty string, return 0;
- any other string will be converted
NaN
- if the string only contains number, will be converted to decimal digits. 0 will be ignored if the string is started with 0.
- If an object, get valueOf() value first, then try to convert it. If get NaN, run toString() method, convert the return value.
parseInt() convert string to number
Rules:
- it will ignore any space until it reach a non-space character.
- if the first character is not number or
-, returnNaN. which meansparseInt("")will returnNaN - if the first character is a number, it will try to parse the next character, until it reaches a non-number character.
123bluewill be converted123,22.5will be converted22 - if string started with
0xit will be converted to a decimal number from hexadecimal number. - if string started with
0, it will be converted to a decimal number, 0 will be ignored.
1 | var num1 = parseInt('1234blue'); //1234 |
You can pass a second paramater
1 | var num = parseInt('af',16) // 175 |
parseFloat()
Rules:
- Almost as same as
parseInt() - the first dot is valid, next are not.
22.34.5will be converted to22.34 parseFloatwill ignore the 0 at the started position.009.9will be converted to9.9- parseFloat will converted the string to decimal number
String
Converted to string
1 | var age = 11; |
null and undefined don’t have toString() method