变量命名及数据类型

131 阅读3分钟

变量的命名

  1. 变量命名规则(必须遵守,不遵守会报错)
  • 由字母、数字、下划线、$符号组成,不能以数字开头
  • 字母区分大小写
  • 不能是关键字和保留字
    • 关键字指的是js中有特殊功能的小词语,比如var、for等
    • 保留字指的是现在没有特殊功能,但是将来新语法中有可能作为关键字使用
  1. 变量命名规范(建议遵守的,不遵守不会报错)
  • 变量名必须有意义
  • 遵守驼峰命名法
        var box = 1
        var str = 1
        var box1 = 1
        var box2 = 1
        var $box2 = 1
        var $box10086 = 1
        var $box_10086 = 1
        
        // var 10086box = 1

        var num = 100
        var NUM = 200
        console.log(num, NUM)

        // 这是一种不规范的书写方式, 示例: 我一把把把把住了
        // var 盒子 = 100
        // console.log(盒子)

        var bigbox = '一个神奇的盒子'
        var bigBox = '一个神奇的盒子'   // 驼峰命名
        var big_box = '一个神奇的盒子'   // 驼峰命名

JS的数据类型

  1. 简单数据类型 (基本数据类型)
    • 数字类型(number): 所有的整数和浮点数

    • 字符串类型(string): 在 JS 中, 只要是引号包裹的内容, 统称为字符串

    • 布尔类型(boolean): 表明 对/错(真/假)

    • undefined类型 (undefined): 表明未定义, 通常我们不会给一个变量赋值为 undefined, 因为 变量定义但不赋值就是一个 undefined

    • null类型(null): 表明为 空对象指针 (空), 一般很少使用, 但是如果创建了一个对象, 但是不知道对象的值是什么的时候可能会给一个 null


        // 数字类型
        var num1 = 1
        var num2 = 100
        var num3 = 10086
        var num4 = 10086.965

        // 字符串类型
        var str1 = 'qwer'
        var str2 = '!@#$%'
        var str3 = '10086'

        // 布尔类型
        var boo1 = true     // 表明为真
        var boo2 = false    // 表明为假

        // undefined 类型
        var und1 = undefined
        var und2

        // null 类型
        var nul = null
  1. 复杂数据类型 (引用数据类型)

数据类型检测

为什么要检测类型?

在 JS 中, 一个变量内部保存的值 是可以任意更改的 所以在使用的时候为了防止因为数据类型不对, 导致的错误, 所以需要在使用前 检测一下数据的类型

数据类型判断的语法

需要借助一个关键字 typeof

假设要检测的变量名为 box

    1. typeof(box)
    1. typeof box
        var box = '我是一个字符串类型的数据'
        // console.log(typeof(box))
        // console.log(typeof box)

        var num = 10086
        // console.log(typeof(num))
        // console.log(typeof num)

        // console.log(typeof('我是一个字符串类型的数据'))
        // console.log(typeof (true))
        // console.log(typeof (undefined))
null 的类型就是 null
  • 但是 typeof 这个方法有一点小问题, 它会将 null 识别为 object 类型

  • 所以在使用 typeof 检测的时候, 不要用来检测 null 这个类型, 因为检测结果不准确

  • 后续的时候会介绍一个方法能够用来检测 null 这个类型

JS 问题

示例: JS 校验 null 的类型方法有哪些

console.log(typeof (null))

数据类型转换

转number类型

JS 转换的时候 任意类型都能够转换为数字使用, 主要是字符串转数字

  1. 借助一个 转型函数 Number(数据)
  2. parseInt(数据)
  3. parseFloat(数据) 参考 上述的 parseInt 自己整理练习
  4. 开发中我最喜欢的一个方式 (变量/数据 - 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


// 2. parseInt
        // 2.1 parseInt 如果传递的是一个数字, 那么会忽略小数点以后的所有内容
        console.log(parseInt(100))
        console.log(parseInt(100.666))

        // 2.2 如果不是数字打头的字符串,会转换为NaN
        console.log(parseInt(''))   // 空字符串 --- NaN
        console.log(parseInt(' '))   // 空白字符串 --- NaN

        console.log(parseInt('!@#$%^&'))   // 非空非空白字符串 --- NaN
        console.log(parseInt('qwerty'))   // 非空非空白字符串 --- NaN
        console.log(parseInt('qwerty10086'))   // 非空非空白字符串 --- NaN

        // 2.3 字符串中,必须是纯数字字符串或者数字字符开头的字符串,才能转换为正常数字,且只取整数部分
        console.log(parseInt('10086qwerty'))   // 字符串中开头为数字, 那么直接保留数字并忽略数字以后的所有内容 --- 10086
        console.log(parseInt('10086qwe999rty'))   // 字符串中开头为数字, 那么直接保留数字并忽略数字以后的所有内容 --- 10086
        console.log(parseInt('100'))   // 直接将字符串中的数字转为 数字类型
        console.log(parseInt('100.10086'))   // 直接将字符串中的数字转为 数字类型


// 3. parseFloat 
    // parseFloat()方法:字符串转浮点数方法
        // 3.1 将字符串转为浮点数数字
        var box2 = '3.14'
        console.log(parseFloat(box2))  //3.14
        // 3.2 满足浮点数数字字符必须在字符串开始,如果不在开始返回值都是NaN
        var box3 = 'Pi-3.14'
        console.log(parseFloat(box2))  //NaN

        // 推荐使用: 数据 - 0;  原因等讲到运算符的时候在解释
        var box = '10086'
        console.log(box)
        console.log(box - 0)

转字符串类型

  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())

        // 布尔值的转换
        console.log(typeof(true), typeof(true.toString()))
        console.log(true, true.toString())

        // undefined 不能使用 toString 去转换
        console.log(undefined.toString())
        var und = undefined
        console.log(und)
        console.log(und.toString())

        // null 也不能通过 toString 转换
        console.log(null.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 + ''))

转布尔类型

  1. 借助一个转型函数 Boolean(变量/数据)

  2. !!变量/数据 一个! 表示得到这个数据取反后的布尔值

    • 一般开发的时候不会主动的转换布尔类型
    • 一般是隐式转换, 也就是由 JS 帮我们完成了数据的转换, 一般做判断的时候比较常见
 // 数字转布尔值         非0即为真
        console.log(Boolean(0))
        console.log(Boolean(1))
        console.log(Boolean(-1))
        console.log(Boolean(100))
        console.log(Boolean(-10000))
        console.log(Boolean(1.567))

        // 字符串转布尔值       只有空字符串会转为 false
        console.log(Boolean(''))
        console.log(Boolean(' '))
        console.log(Boolean('1234567'))
        console.log(Boolean('!@#$'))
        console.log(Boolean('qwerts'))
        console.log(Boolean('你好!@#$%qwert12345'))

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


        console.log(!undefined) // true
        console.log(!!undefined)    // false