javascript-data-type

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
2
3
var message = 'some string';
alert(typeof message); // 'string'
alert(typeof 96); // 'number'

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
2
var message;
alert(message == undefined); //true;
1
2
3
4
var message;
// var age;
alert(message); //undefined;
alert(age); //error;

but here is another example.

1
2
3
4
var message;
// var age;
alert(typeof message); //'undefined'
alert(typeof age); // 'undefined'

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
2
var message = 'Hello world';
var messageAsBoolean = Boolean(message);
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
2
3
var octalNum1 = 070;
var octalNum2 = 079; // invalid octal number, 0 will be ignored. the final result is 79
var octalNum3 = 08; // invalid octal number

Number range

We could get max and min number by

1
2
Number.MIN_VALUE;
Number.MAX_VALUE;

Any number is greater or less than max or min value will be converted to

1
2
Infinity;
-Infinity;

isFinite() could be used to check if the nubmer is finite or not

1
2
var result = Number.MAX_VALUE + Number.MAX_VALUE;
alert(isFinite(result)); //false;

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
2
3
4
5
alert(isNaN(NaN)); //true;
alert(isNaN(10)); //false;
alert(isNaN("10")); //false;
alert(isNaN("blue")); //true;
alert(isNaN("true")); //false;

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, return NaN
  • 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. 011 will be converted to 11, 00000001 will be converted to 1
    • 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 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 - , return NaN. which means parseInt("") will return NaN
  • if the first character is a number, it will try to parse the next character, until it reaches a non-number character. 123blue will be converted 123, 22.5 will be converted 22
  • if string started with 0x it 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
2
3
4
5
6
7
var num1 = parseInt('1234blue'); //1234
var num2 = parseInt(""); //NaN
var num2 = parseInt("0xA"); // 10
var num2 = parseInt(22.5); //22
var num2 = parseInt("070"); //70
var num2 = parseInt("70"); //70
var num2 = parseInt("0xf"); //15

You can pass a second paramater

1
2
var num = parseInt('af',16) // 175
var num = parseInt('Af',16) //175

parseFloat()

Rules:

  • Almost as same as parseInt()
  • the first dot is valid, next are not. 22.34.5 will be converted to 22.34
  • parseFloat will ignore the 0 at the started position. 009.9 will be converted to 9.9
  • parseFloat will converted the string to decimal number

String

Converted to string

1
2
3
4
5
6
7
8
9
10
var age = 11;
var ageAsString = age.toString();

var num = 10;
alert(num.toString()); // "10";
alert(num.toString(2)); //"1010";
alert(num.toString(8)); //"12"
alert(num.toString(16)); //"a"
var value = true;
alert(value.toString()); // "true"

null and undefined don’t have toString() method