There are seven data types in ECMAScript . Six of them are simple data types , you can call them primitive types : Undefined , Null , Boolean , Number , String and Function . One is complex data type : Object . Maybe you would ask what about Array , custom objects and others . Well , good question . Because I am talking about data types , more specificly , data types in ECMAScript . I will explain later .
As you know I split data types into two part . So I will explain them with two part .
First , primitive types . These are the simple data types . Like data types in C++ . These are basic . You can test any statements with typeof operator , and then it will show you one of them , and just seven of them .
Undefined : Just one value , undefined . 1. When a variable is declared using var but not initialized , it is assigned the value of undefined . 2. When you use a variable which is not declared , it is assigned the value of undefined .
Null : Just Like Undefined , one value , null . It is a value of an empty object pointer . So this why you typeof a variable which the value is null will return "object" . Like :
var nullType = null;
console.log(nullType); // object
Boolean : Like C++ , have two values : true and false . And one thing I have to mention is Boolean() . It is a function that you can reverse some variables not type of boolean to boolean . Here are the rules :
Boolean | true | false |
String | Any not empty string | "" |
Number | Not Zero | 0 & NaN |
Object | Any object | null |
Undefined | N/A | undefined |
Number : Included Int , Float and NaN . I will explain the NaN , AKA , not a number . It is not equal to anything , included itself .
String : You can define a string like : | var string = "" ; | . So simple . But here is one function I want to talk about : toString() . It is a function of String . Means you can call it with any variable , like : | myVariable.toString(); | . Then it will return a string of the variable by some rules . You can just think it in this way : return the string that the value of it . Like boolean : a true variable return "true" .
Function : You know , just simple , it will return function with using the typeof operator when it is a function .
OK , I explained six simple data types . Next , I will explain the last one , Object .
Object : It includes Object , Array , custom object ... But I just show you the code .
var undefinedType = undefined;
var nullType = null;
var booleanType = true;
var numberType = 2;
var stringType = "data type";
var functionType = function() {
alert("Data Type");
};
var objectType = new Object();
var arrayType = new Array();
var customObjectType = {
name: "Root"
};
console.log(typeof undefinedType); // undefined
console.log(typeof nullType); // object
console.log(typeof booleanType); // boolean
console.log(typeof numberType); // number
console.log(typeof stringType); // string
console.log(typeof functionType); // function
console.log(typeof objectType); // object
console.log(typeof arrayType); // object
console.log(typeof customObjectType); // object
So I hope this will help you . Good luck .