JavaScript 数据类型

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript">

        /**
         * 数据类型指的是字面量的类型
         * 在JS中一共有六种数据类型
         *      String 字符串
         *      Number 数值
         *      Boolean 布尔值
         *      Null 空值
         *      Undefined 未定义
         *      Object 对象
         * 
         * 其中 String Number Boolean Null Undefined 属于基本数据类型
         *     Object 属于引用数据类型
         */

        /**
         * String 字符串
         *  - 在JS中字符串需要使用引号引起来
         *  - 使用双引号或单引号都可以,但是不要混着用
         *  - 引号不能嵌套,双引号里不能放双引号,单引号里不能放单引号
         *  - 在字符串中可以使用 \ 作为转义字符,当表示一些特殊符号时可以使用进行\转译
         *    \“ 表示 ”
         *    \‘ 表示 ’
         *    \n 表示 换行
         *    \t 表示 制表符
         *    \\ 表示 \
         */


        /**
         * 在JS中所有的数值都是Number类型
         *  包括整数和浮点数
         * 
         * JS 中可以表示的数字最大值 Number.MAX_VALUE = 1.7976931348623157e+308
         *  若使用 Number 表示的数字超过最大值,则会返回一个 Infinity 表示正无穷
         *  使用 typeof 检查 Infinity 也会返回 Number
         * 
         * JS 中可以表示的数字最小值 Number.MIN_VALUE = 5e-324 (这里表示的是除了0以外绝对值的最小值)
         * 
         * NaN 也是一个特殊的数字,表示 Not a Number
         *  使用 typeof 检查 NaN 也会返回 Number
         */

        // 数字 123
        var a = 123;
        // 字符串 “123”
        var b = "123";

        /**
         * 可以使用一个运算符 typeof 来检查一个变量的类型
         */
        console.log(typeof a); // string
        console.log(typeof b); // number

        console.log(Number.MAX_VALUE);
        console.log(Number.MAX_VALUE + 1);
        console.log(Number.MIN_VALUE);

        // JS 中整数的运算基本可以保证精确
        var c = 12345 + 67890;
        console.log("12345 + 67890 = " + c);

        // JS 中进行浮点数运算,可能会得到一个不精确的结果
        // 所以千万不要使用 JS 进行对精确度要求较高的运算
        var d = 0.1 + 0.2; // 0.30000000000000004
        console.log("0.1 + 0.2 = " + d);


        /**
         * Boolean 布尔值
         *  布尔值只有两个,主要用来做逻辑判断
         *  true - 真
         *  false - 假
         * 
         * 使用 typeof 检查一个布尔值时,会返回 boolean
         */
        var bool = true;
        console.log(bool);
        console.log(typeof bool);


        /**
         * Null 空值 && Undefined 未定义
         * 
         * Null 类型的值只有一个,就是 null
         *  null 这个值专门用来表示一个为空的对象
         *  使用 typeof 检查一个 null 值时,会返回 object
         * 
         * Undefined 类型的值只有一个,就是 undefined
         *  当声明一个变量,但是并不给变量赋值时,它的值就是 undefined
         * 使用 typeof 检查一个 undefined 值时,会返回 undefined
         */

        var e = null;
        console.log(e); // null
        console.log(typeof e); // object

        var f;
        console.log(f); // undefined
        console.log(typeof f); // undefined


        /**
         * 强制类型转换
         *  - 指将一个数据类型强制转换为其他的数据类型
         *  - 类型转换主要指:将其他的数据类型转换为 String Number Boolean
         */

        /**
         * 将其他的数据类型转换为 String
         * 
         * 方法1. 
         *  - 调用被转换数据类型的 toString() 方法
         *  - 该方法不会影响到原变量,它会将转换后的结果返回
         *  - 但是注意:null 和 undefined 没有 toString 方法(调用他们的 toString 方法会报错)
         * 
         * 方法2. 
         *  - 调用 String() 函数,并将被转换的数据作为参数传递
         *  - 使用 String() 函数做强制类型转换时,对于 Number 和 Boolean 实际上就是调用 toString() 方法。
         *    但是对于 null 和 undefined,就不会调用 toString() 方法,
         *    而是直接进行转换 null -> "null"  undefined ->"undefined"
         */

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

        // 方法1. 调用被转换数据类型的 toString() 方法
        var str1 = num.toString();
        console.log(typeof str1);
        console.log(str1);

        // 方法2. 调用 String() 函数,并将被转换的数据作为参数传递
        var str2 = String(num);
        console.log(typeof str2);
        console.log(str2);

        /**
         * 将其他的数据类型转换为 Number
         * 
         * 方法1. 使用 Number() 函数
         *  - 字符串 --> 数字
         *    1 如果是纯数字的字符串,则直接将其转换为数字
         *    2 如果字符串中有非数字的话,则转换为 NaN
         *    3 如果字符串是一个空串或是一个全是空格的字符串,则转换为 0
         *  
	     * - 布尔 --> 数字
         *    true --> 1
         *    false --> 0
         *  
	     * - Null --> 0
         *  
	     * - undefined-- > NaN
         * 
	     * 方法2. parseInt() parseFloat() 专门用来对付字符串
         *       parseInt() 可以将一个字符串中的有效整数内容取出来,然后转换为 Number
         *       parseFloat() 作用和 parseInt() 类似,不同的是它可以获得有效的小数
         *       如果对一个非 String 使用 parseInt() 或 parseFloat(),它会先将其转换为 String 然后再操作
         */

        // 1 如果是纯数字的字符串,则直接将其转换为数字
        var a = "789";

        // 调用 Number() 函数来将a转换为Number类型
        a = Number(a);
        console.log("字符串转换为数字");
        console.log(typeof a);
        console.log(a);

        // 2 如果字符串中有非数字的话,则转换为 NaN
        var a = "789abc";

        a = Number(a);
        console.log(typeof a);
        console.log(a);

        //  3 如果字符串是一个空串或是一个全是空格的字符串,则转换为 0
        var a = "";

        a = Number(a);
        console.log(typeof a);
        console.log(a);

        // paiseInt()
        a = "321px";

        a = parseInt(a);
        console.log(typeof a);
        console.log(a);

        /**
         * 其他进制的数字
         * 在 js 中,需要表示16进制的数字,则需要以 0x 开头
         * 	         表示8进制的数字,则需要以 0 开头
         *               表示2进制的数字,则需要以 0b 开头
         */

        a = 0x10;
        console.log(a);

        a = 010;
        console.log(a);

        a = 0b1101;
        console.log(a);

        /**
         * 将其他的数据类型转换为 Boolean
         *  - 使用 Boolean() 函数
         *    1 Number --> Boolean  除了 0 和 NaN 都是 true
         *    2 String --> Boolean  除了 "" 都是 true
         *    3 null 和 undefined 都会转换为 false
         *    4 Object 也会转换为 true
         */

        var a = 123;  // true
        a = -123;     // true
        a = 0;        // false
        a = Infinity; // true
        a = NaN;      // false

        a = "hello";  // true
        a = "";       // false

        a = null;     // false

        a = undefined;// false

        console.log(typeof a);
        console.log(a);

        // 调用 Boolean() 函数
        a = Boolean(a);
        console.log(a);
        
    </script>
</head>

<body>
    javascript 数据类型
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值