03-JavaScript数据类型

2018-11-05  本文已影响0人  喝酸奶要舔盖__

JavaScript数据类型

var num = 10.3
console.log(typeof num);

字符串类型

注意点:
在JS中单引号双引号可以嵌套
不同类型可以嵌套, 相同类型不能嵌套

var str3 = "hello "nj""; // 错误
var str4 = `hello `nj``; // 错误
var str5 = "hello 'nj'"; // 正确
var str6 = `hello "nj"`;// 正确

Number类型

Number类型注意点
JS中整数的运算可以保证精确的结果
在JS中浮点数的运算可能得到一个不精确的结果

Boolean 布尔值

var bool3 = Boolean(0);
console.log(bool3); // false
var bool4 = Boolean(1);
console.log(bool4); // true
var bool5 = Boolean(-1);
console.log(bool4); // true
var bool6 = Boolean(Infinity);
console.log(bool4); // true
var bool7 = Boolean(-Infinity);
console.log(bool4); // true
var bool8 = Boolean(NaN);
console.log(bool8); // false
var bool9 = Boolean(undefined);
console.log(bool8); // false
var bool10 = Boolean(null);
console.log(bool8); // false
var bool11 = Boolean("");
console.log(bool8); // false
var bool12 = Boolean("abc");
console.log(bool12); // true

Null和Undefined

var value1 = undefined;
console.log(typeof value); //结果是undefined

var value2;
console.log(typeof  value2); //结果是undefined
var test1 = null;
var test2 = undefined;
console.log(test1 == test2);
console.log(test1 === test2);

转换为字符串类型

var num = 123
console.log(num.toString());
console.log(typeof num.toString());
var flag = true;
console.log(flag.toString());
console.log(typeof flag.toString());
var num = 10;
console.log(num.toString(2));//转换为2进制
console.log(num.toString(8));//转换为8进制
console.log(num.toString(16));//转换为16进制
var n = null;
var u = undefined;
console.log(String(n));
console.log(String(u));
var res1 = 10 + "";
console.log(res1);

var res2 = true + "";
console.log(res2);

var res3 = null + "";
console.log(res3);
console.log(typeof res3);

var res4 = undefined + "";
console.log(res4);
console.log(typeof res4);

转换为Number类型

var str = "123";
var res1 = Number(str);
console.log(res1);
var str2 = "3.14aa";
var res2 = Number(str2);
console.log(res2);

var res6 = Number("");
console.log(res6);

var res7 = Number("  ");
console.log(res7);
var flag = true;
var res3 = Number(flag);
console.log(res3);
var n = null;
var i = undefined;
var res4 = Number(n);
console.log(res4); //0
console.log(typeof res4);
var res5 = Number(i);
console.log(res5); //NaN
console.log(typeof res5);

注意点:
+加号不会改变数据的正负性
-减号会改变数据的正负性

    var str1 = "123";
    var str2 = "-3.14";
    var flag1 = true;
    var flag2 = false;
    var n = null;
    var u = undefined;

    var res1 = +str1;
    console.log(res1);
    console.log(typeof res1);

    var res2 = -str2;
    console.log(res2);
    console.log(typeof res2);

    var res3 = +flag1;
    console.log(res3);
    console.log(typeof res3);

    var res4 = +flag2;
    console.log(res4);
    console.log(typeof res4);

    var res5 = +n;
    console.log(res5);
    console.log(typeof res5);

    var res6 = +u;
    console.log(res6);
    console.log(typeof res6);

注意点:
parseInt/parseFloat , 会从左至右的提取, 一旦被中断后续的就提取不到了
对于parseFloat来说, 只能提取一个小数点, 第二个小数点会被中断

    var str = "3.14px";
    var res = Number(str);
    console.log(res);//NaN

    var str2 = "3.1410px20px";
    var res2 = parseInt(str2);
    console.log(res2);

    var str3 = "3.14.15px";
    var res3 = parseFloat(str3);
    console.log(res3);

转换为Boolean类型

    var str1 = '';
    var str2 = ' ';
    var str3 = '123';
    var res1 = Boolean(str1);
    console.log(res1);
    var res2 = Boolean(str2);
    console.log(res2);
    var res3 = Boolean(str3);
    console.log(res3);
    var num = 0;
    var num2 = -1;
    console.log(Boolean(num));
    console.log(Boolean(num2));
    var n = null;
    var u = undefined;
    console.log(Boolean(n));
    console.log(Boolean(u));
    var num3 = NaN;
    var res4 = Boolean(num3);
    console.log(res4);

总结:
"" / 0 / null / undefined / NaN --> false

上一篇下一篇

猜你喜欢

热点阅读