js數(shù)據(jù)類型分為倆種 1、基本數(shù)據(jù)類型 2嗅榕、復(fù)雜數(shù)據(jù)類型
首先介紹一下基本數(shù)據(jù)類型吟逝,它分五種
undefined --未定義類型
undefined類型只有一個值,即特殊的undefined的。
1) 1)使用var聲明變量但未賦值時料祠,這個變量的值就是undefined顺呕。檢測其類型也是 undefined蹦掐。
列如我們
var a鳖粟;
console.log(a)张漂;//a=undefined
console.log(typeof a)漠畜;//"undefined"
2)變量從未定義過 ,他的類型為undefined 瘾敢,值報錯典蜕。
console.log(b)嘉裤; //報錯 b is not defined
console.log(typeof b);//"undefined"
null --空類型
他也是只有一個值,這個特殊值是null陕靠,null值表示一個空對象指針,它的特殊對象類型是object净捅。
var aaa = null捉腥;
console.log(aaa)氓拼;//null
console.log(typeof aaa);//"object"
Bollean - - 布爾類型
Boolean類型只有兩個值:true(真) \ false(假)抵碟。
如果轉(zhuǎn)換成數(shù)值的話桃漾,我們可以把true當(dāng)作1,false當(dāng)作0拟逮,
console.log(true==1); //true
console.log(false==0); //true
var str = "hello world!"撬统;
console.log(Boolean(str));//true
1敦迄、除0和NaN之外的所有數(shù)字恋追,轉(zhuǎn)換為布爾型都為true
2、除"" 之外的所有字符罚屋,轉(zhuǎn)換為布爾型都為true
3苦囱、null和undefined轉(zhuǎn)換為布爾型為false
Number--數(shù)值類型
NaN:not a Number 不是一個數(shù)字 特殊的數(shù)值類型
表示一個本來要返回數(shù)值的操作數(shù)未返回數(shù)值的情況。
NaN特點:1)任何涉及NaN的操作都會返回NaN脾猛。
- NaN與任何值都不相等撕彤,包括NaN本身。
alert( NaN == NaN ) //false
isNaN:判斷參數(shù)是否"不是數(shù)值"猛拴。如果是數(shù)值返回false,不是數(shù)值返回true羹铅;
alert(isNaN(NaN)); //true
alert(isNaN(10))愉昆; //false(10是一個數(shù)值)
alert(isNaN("10"))职员; //false(可以被轉(zhuǎn)換成數(shù)值10)
alert(isNaN("hello")); //true(不能轉(zhuǎn)換成數(shù)值)
alert(isNaN(true))撼唾; //false(可以被轉(zhuǎn)換成數(shù)值1)
數(shù)值轉(zhuǎn)換:有三個函數(shù)可以把非數(shù)值轉(zhuǎn)換為數(shù)值:
1)Number()
2)parseInt() //取整數(shù)
3)parseFloat() //取從開始第一個小數(shù)點后面的數(shù)
前提開頭必須是數(shù)字
列如
parseInt():從第一位起取到整數(shù)部分
parseInt("123456green") //123456
parseInt("") //NaN ( 區(qū)別:Number("") //0)
parseInt(" 11abc") //11
parseInt("22.5212") //22
parseFloat():從第一位起取到小數(shù)部分(只識別第一個小數(shù)點)廉邑,只解析10進制數(shù)。
parseFloat("12345yellow") //123456
parseFloat("22.52") //22.52
parseFloat("22.5.22") //22.5
Number():轉(zhuǎn)型函數(shù)Number()可以用于任何數(shù)據(jù)類型
Number(true) //1
Number(false) //0
Number(22) //22
Number(null) //0
Number(undefined) //NaN
Number("123") //123
Number("2.11") //2.11
Number("") //0
Number("11asdf") //NaN
String -- 字符串類型
字符串可以由雙引號或單引號表示
var str = "hello world";
var str = 'hello world';
查看字符串的長度:length屬性
var str = "hello world";
console.log(str.length);//11
空格也算1個長度
查看字符串相應(yīng)位置的字符倒谷。
var str = "hello world";
console.log(str[1]);//e
是從0開始數(shù)
轉(zhuǎn)化為字符串的2種方法
toString()
數(shù)組蛛蒙、布爾值、對象和字符串值都有toString()方法渤愁。
但null和undefined值沒有這個方法牵祟。
String(變量)
強轉(zhuǎn)函數(shù),可以轉(zhuǎn)化任何類型的值
var num1;
String(123) // "123"
String(3>4) // "false"
String(3<4) // "true"
String(num1) // "undefined"
String(null) // "null"
* "+"也可以把某個值轉(zhuǎn)換為字符串抖格,也可以拼接多個字符串诺苹。
var aaa = {
taocan: '火雞面',
price: 98,
title: 2019,
}
var taocan ="火雞面";
var price = 98;
var title = 2019;
console.log([title]+`年`+[price]+`元買了一碗`+[taocan])
第二種
console.log(`${aaa.title}年${aaa.price}元買了${aaa.taocan}`);
效果如下
2019年98元買了一碗火雞面
2019年98元買了火雞面
Object -- 類型 :
ECMAScript中的對象其實就是一組數(shù)據(jù)和功能的集合
var o = {}; \\對象字面量方法創(chuàng)建
var o = new Object(); \\構(gòu)造函數(shù)方法創(chuàng)建
typeof 的類型
有6種類型:Boolean 、String雹拄、Object收奔、Number、Function滓玖、Undefined
var bb = function(){
};
console.log(typeof bb); //"function"
var cc = 2;
console.log(typeof cc); //"number"
var dd ;
console.log(typeof dd); //"undefined"
var qq = "d222";
console.log(typeof qq);//"string"
var obj = {
key: value
}
var value = "adsqdssa"
console.log(typeof obj);//"object"
var ww=10;
var qq=(isNaN(ww));
console.log(qq); //false;
console.log(typeof qq); //"boolean"