關於js的數據類型

一:js的數據類型

1:基本數據類型:number string boolean null undefined

2:複雜數據類型:Array Function Objeck Data Math RegExp(正則表達式) Number String Boolean

二:檢測的方法

1:數據類型
typeof :檢測所有的基本數據類型的方法,除null 之外,還有function
用法:typeof(需要檢測的數據類型)
2檢測數組
第一種:isArray()
// isArray()
var arr = [10,20,30];
console.log(Array.isArray(arr));//true
第二種:instanceof :用來檢測數組不是很嚴謹
// instanceof()
var arr1 = [10,20,30];
console.log(arr1 instanceof Array);//true
第三種 toString();
用法:Object.prototype.toString.call(需要檢測的數據類型)
// toString()
console.log(Object.prototype.toString.call(1));//'[object Number]';
console.log(Object.prototype.toString.call('a'));//'[object String]';
console.log(Object.prototype.toString.call(true));//'[object Boolean]';
console.log(Object.prototype.toString.call(undefined));//'[object undefined]';
console.log(Object.prototype.toString.call(null));//'[object null]';
console.log(Object.prototype.toString.call([1,2,3]));//'[object Array]';
console.log(Object.prototype.toString.call(new Object());//'[object Object]';
console.log(Object.prototype.toString.call(function(){});//'[object function]';

三 :關於null not is anumber(不是一個數字)

    //不是一個類型,是一個數值, 是number數據類型中的一個數值.
    isNaN(); 用來判斷一個數值是否是數字.
    //如果你是一個數字,就不是一個NaN,那麼isNaN就是一個false.
    console.log(isNaN(100)); //false
    //如果你是不是一個數字,就是一個NaN,那麼isNaN就是一個true
    console.log(isNaN('abc'));//true
    //檢測的時候,會有一個隱式類型轉換
    console.log(isNaN('100'));//false

四:基本包裝類型–>Number

// var num = 123;
    // num.toString() //爲什麼num是一個基本數據類型,能點出方法來?
    //系統會做一個事情.
     var num = new Number(123);
     console.log(num);
     console.log(num.toString()); //'123'
    // //如果要取到基本包裝類型中的真實的數值,使用valueOf;
     console.log(num.valueOf()); //123

五:Boolean

 var b1 = true;
 b1.toString();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章