数据类型与运算符

142 阅读5分钟

一:数据类型 数据分为两种:1基本数据类型 2引用数据类型 1:基本数据类型分为(1,数值类型 2,字符串类型 3,布尔类型 4,空类型(空类型又分为 undefined 和null)) 1数值类型(number):常见的的正数负数.小数(浮点数) 科学计数法 进制计数法(0x代表十六进制,0o代表的是八进制,0b开头代表的是二进制 ) 2:字符串类型(string):在js内一切以单引号和双引号包裹的内容都是字符串类型 (js中不区分单引号双引号 ,在一个字符串里面前后配套就可以) 3:布尔类型(bolean):有两个值分别为true和false true:代表真 false:代表假 4:空类型:分为两种null和undefined null(object) (表明为 空对象指针 (空), 一般很少使用, 但是如果创建了一个对象, 但是不知道对象的值是什么的时候可能会给一个 null )

                     undefinedundefined

(undefined类型 (undefined): 表明未定义, 通常我们不会给一个变量赋值为 undefined, 因为 变量定义但不赋值就是一个 undefined) 二 检测数据类型: 关键字typeof 语法:typeof 后面跟上你要检测的变量 结果:该变量存储的数据的数据类型 返回的检测结果:

三 数据类型的转换 1 转数值:

  1. Number(数据)方法     * 转型函数Number()可以用于任何数据类型,将其他数据类型转为数字     * 字符串:纯数字字符串转为对应数字,空字符串和空白字符串转为0,非空非纯数字字符串转为NaN     * 布尔值:true转为1,false转为0     * undefined:转为NaN     * null:转为0

  2. parseInt()方法:字符串转整数方法     * 对浮点数进行取整操作         * 对数字取整直接舍弃小数部分,只保留整数     * 将字符串转为整数数字         * 将字符串转为整数数字,也包含取整功能     * 字符串中,必须是纯数字字符串或者数字字符开头的字符串,才能转换为正常数字,且只取整数部分     * 如果不是数字打头的字符串,会转换为NaN

  3. parseFloat()方法:字符串转浮点数方法     * 将字符串转为浮点数数字     * 满足浮点数数字字符必须在字符串开始,如果不在开始返回值都是NaN

4 老师最喜欢的 数据 - 0 例如: // var box = '10086'         // console.log(box)         // console.log(box - 0) 2:转字符串; 转字符串类型            1. 变量/数据.toString()                    问题: undefined 和 null 不能使用                   2. String(变量/数据)                  什么类型都能转换为 字符串                    3.  变量/数据 + ''                    原理讲到 运算符的时候再说 1变量/数据.toString()  var box = 100         console.log('box 原本的值: ', typeof (box), box)         console.log('box 转换数据类型后: ', typeof (box.toString()), box.toString() ) 图片里是布尔类型和空类型的两个转换字符串

2 String(变量/数据)(可以使空元素的两种类型显现) console.log(typeof(undefined), typeof(String(undefined)))         // console.log(typeof(null), typeof(String(null)))

        // console.log(typeof(String(100)))         // console.log(typeof(String(true))) 3 变量/数据 +''(也可以使空元素的两种类型显现)  console.log(typeof(undefined), typeof(undefined + ''))         // console.log(typeof(null), typeof(null + '')) 3 转换boolean(布尔)类型、 转布尔类型          *      一般开发的时候不会主动的转换布尔类型          *      一般是隐士转换, 也就是由JS帮我们完成了数据的转换为布尔值, 一般做判断的时候比较常见          *          *      1. 借助一个转型函数 Boolean(变量/数据)          *          *      2. !!变量/数据          *          一个! 表示得到这个数据取反后的布尔值 a: // 数字转布尔值         非0即为真(只要不是0都是true)         // console.log(Boolean(0)) (false)         // console.log(Boolean(1))(true)         // console.log(Boolean(-1))         // console.log(Boolean(100))         // console.log(Boolean(-10000))         // console.log(Boolean(1.567)) b:// 字符串转换为布尔:只有空字符才是false         // console.log(Boolean(''))         // console.log(Boolean(' '))         // console.log(Boolean('122334'))         // console.log(Boolean('adsadd'))         // console.log(Boolean('@#%^&*()'))         // console.log(Boolean('sa;fos345@#')) c :空元素转换成布尔 // undefined和null:都是假的(false)         // console.log(Boolean(undefined))         // console.log(Boolean(null))  // 第二种方法:!!数据/变量(一个感叹号会得到相反的)         // console.log(Boolean(!null))         // console.log(Boolean(!undefined))         // console.log(Boolean(!!null))         // console.log(Boolean(!!undefined)) 四 运算符 1算术运算符  JS 的运算符          *          *      也叫 操作符, 是 JS 中 发起一个运算的最简单的方式          *          *  算数运算符          *      +   -  *  /  %          *      加减乘除取余          *          *  运算符本身就是给 数字类型使用的         */ 加号

  • 一般是给数字使用的, 但是如果 符号的任意一边有字符串类型的, 那么不在计算求和, 而是计算一个拼接          *      并且拼接后的值是字符串类型的 减号
  • 就是将 符号两边的值 相减 得到一个新的值          *          *      运算的时候, 如果符号两边有字符串. 那么 JS 会将 字符串转换为 数字 然后参与运算          *          *      这也是为什么 数据 - 0 能够转换为 number 类型

2 赋值运算符  *  赋值运算符          *          *      =   赋值号          *          *      +=      当要给一个变量重新赋值, 赋值为他本身加一个内容, 就可以使用 +=         */

3 比较运算符    *  比较运算符          *          *      大于            >          *      小于            <          *      大于等于        >=          *      小于等于        <=          *          *      等于          *          ==      对比两边是否相等, 不会区分数据类型      了解或者特殊情况下书写          *          ===     对比两边是否相等, 区分数据类型          推荐写 ===          *          一定要注意, 等于 最少要两个 ==          千万不要写成 =          *          *      不等于          *          !=      对比两边是否不相等, 不会区分数据类型          *          !==     对比两边是否不相等, 区分数据类型            推荐写 !==         */