js分享02(小白必看)

140 阅读4分钟

js 数据类型

JS中的值,无论是字面量还是变量,都有明确的类型

  • 数据类型分类(以基本数据类型为主)
    • Number 数字类型
      • 不区分整数、浮点数、特殊值,都是 Number 类型
    • String 字符串类型
      • 所有的字符串都是 String 类型
    • undefined undefined类型
      • ndefined本身就是一个数据,表示未定义,变量只声明不赋值的时候,值默认是 undefined
    • Boolean 布尔类型
      • Boolean 字面量:只有 true 和 false 两个字面量的值,必须是小写字母
      • 计算机内部存储:true 为 1,false 为 0
    • null null类型
      • null 本身就是一个数据
      • 从逻辑角度,null 值表示一个空对象指针
      • 如果定义的变量准备在将来用于保存对象,最好该变量初始化为 null
    • Object 对象类型(后续课程详细讲解)

数据类型检测

  • 为什么要有数据类型检测?
    • JS语言是一门动态类型的语言,变量并没有一个单独的数据类型,而是会随着内部存储数据的变化,数据类型也会发生变化
    • 变量的数据类型,与内部存储数据有关
    • 将来使用变量时,需要知道内部存储的数据是什么类型,避免程序出错
  • 使用 typeof 的方法进行数据检测
    • 检测方式:在 typeof 后面加小括号执行,将要检测的数据放在小括号内部

数据类型转换(转数值 / 转字符串 / 转布尔)

转数值

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

        var box = '10086'

        console.log('box 原本的数据类型', typeof(box), box)

        var newBox = Number(box)

        console.log('box 通过转型函数转换后的值', typeof(newBox), newBox)


        // 1.1 字符串:纯数字字符串转为对应数字,空字符串和空白字符串转为0,非空非纯数字字符串转为NaN

        console.log(Number('')) // 空字符串 --- 0

        console.log(Number(' ')) // 空白字符串 --- 0

        console.log(Number('qwertyui')) // 没有数字的字符串 --- NaN     此时已经将这个字符串转换为数字类型了, 但是没有一个合适的数字能够表达他, 所以使用一个统一的 NaN

        console.log(Number("123456789"))    // 只有数字的字符串 --- 123456789       直接转换成数字了


        // 1.2 布尔值:true转为1,false转为0

        console.log(Number(true))    // 1

        console.log(Number(false))   // 0


        // 1.3 undefined:转为NaN null:转为0

        console.log(Number(undefined))   // NaN

        console.log(Number(null))    // 0
  1. parseInt()方法:字符串转整数方法
    • 对浮点数进行取整操作
      • 对数字取整直接舍弃小数部分,只保留整数
    • 将字符串转为整数数字
      • 将字符串转为整数数字,也包含取整功能
    • 字符串中,必须是纯数字字符串或者数字字符开头的字符串,才能转换为正常数字,且只取整数部分
    • 如果不是数字打头的字符串,会转换为NaN
  2. parseFloat()方法:字符串转浮点数方法
    • 将字符串转为浮点数数字
    • 满足浮点数数字字符必须在字符串开始,如果不在开始返回值都是NaN
        console.log(parseFloat(111.234))//保留小数部分
        
        console.log(parseFloat(''))//NaN
        
        console.log(parseFloat(' '))//NaN
        
        console.log(parseFloat('qwertyuio'))//NaN
        
        console.log(parseFloat('sdfvdfv1222.342'))//NaN
        
        console.log(parseFloat('1222.342sd'))//1222.342 数字类型
                
        console.log(parseFloat('1.23422'))//转换为数字类型

转字符串

  1. 变量.toString()方法
1.变量/数据.toString

        var box = 10
        
        console.log('box :',typeof(box),box)
        
        console.log('box:',typeof(box.toString()),box.toString())
        
        // 布尔
        
        console.log(typeof(true),typeof(true.toString()))
        
        console.log(typeof(false),typeof(true.toString()))
        
        //未定义  无法使用.toString
        
        console.log(typeof(undefined))
        
        console.log(typeof(undefined.toString()))
        
        //null   无法使用.toString
        
        console.log(typeof null)
        
        console.log(typeof(null.toString()))
  1. String(变量)方法,有些值没有toString(),这个时候可以使用String()。比如undefinednull
        console.log(typeof(String(undefined)))

        console.log(typeof(String(null)))

        console.log(typeof(String(100)))

        console.log(typeof(String(true)))
  1. + 号拼接字符串方式
    • num + “” ,当 + 两边一个操作符是字符串类型,一个操作符是其他类型的时候,会先把其他类型转换成字符串在进行字符串拼接,返回字符串

转布尔

  1. Boolean(变量)方法
    • 转型函数Boolean()可以用于任何数据类型,将其他数据类型转为布尔类型的值
    • 转为false:NaN、0、“”空字符串、null、undefined
    • 转为true:非0 非NaN数字、非空字符串
转布尔

        console.log(Boolean(0))//false

        console.log(Boolean(undefined))//false

        console.log(Boolean(""))//false

        console.log(Boolean(" "))//true

        console.log(Boolean("123434"))//true

        console.log(Boolean("wswwroeirhwe"))//true

        console.log(!undefined)//true

        console.log(!!undefined)//false

运算符

运算符

        +

        console.log(1 + 1)

        console.log(1 + '1')

        // -

        console.log(1 - 1)

        console.log(1 - '1')

        // *

        console.log(2 * 3)

        console.log(2 * '3')

        // /

        console.log(6 / 3)

        console.log(6 / '3')

        // %

        console.log(10 % 3)

        console.log(10 % '3')

赋值运算符

赋值运算

        var a = 10;

        var b = 20;

        var c = 30;

        var d = 40;

        var e = 10;

        a += 100;

        b -= 5;

        c *= 2;

        d /= 10;

        e %= 3;

        console.log(a, b, c, d, e)

比较运算符

// 比较运算符

        // == & ===

        console.log(1 == 1) //true

        console.log(1 === 1) //true

        console.log(1 == '1') //true

        console.log(1 === '1') //false

        // != & !==

        console.log(1 != 2) //true

        console.log(1 !== 2) //true

        console.log(1 != '1') //false

        console.log(1 !== '1') //true