变量的命名
变量命名规则(必须遵守,不遵守会报错)
- 由字母、数字、下划线、$符号组成,不能以数字开头
- 字母区分大小写
- 不能是关键字和保留字
- 关键字指的是js中有特殊功能的小词语,比如var、for等
- 保留字指的是现在没有特殊功能,但是将来新语法中有可能作为关键字使用
- 变量命名规范(建议遵守的,不遵守不会报错)
var box = 1
var str = 1
var box1 = 1
var box2 = 1
var $box2 = 1
var $box10086 = 1
var $box_10086 = 1
var num = 100
var NUM = 200
console.log(num, NUM)
var bigbox = '一个神奇的盒子'
var bigBox = '一个神奇的盒子'
var big_box = '一个神奇的盒子'
简单数据类型 (基本数据类型)
- 1.1 数字类型(number): 所有的整数和浮点数
- 1.2 字符串类型(string): 在 JS 中, 只要是引号包裹的内容, 统称为字符串
- 1.3 布尔类型(boolean): 表明 对/错(真/假)
- 1.4 undefined类型 (undefined): 表明未定义, 通常我们不会给一个变量赋值为 undefined, 因为 变量定义但不赋值就是一个 undefined
- 1.5 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
var und1 = undefined
var und2
var nul = null
数据类型检测
- 为什么要检测类型?
- 在 JS 中, 一个变量内部保存的值 是可以任意更改的
- 所以在使用的时候为了放置 因为数据类型不对, 导致的错误, 所以需要在使用前 检测一下数据的类型
- 数据类型判断的语法
- 需要借助一个关键字 typeof
- 假设要检测的变量名为 box =====> typeof(box)
var box = '我是一个字符串类型的数据'
- null 的类型就是 null
- 但是 typeof 这个方法有一点小问题, 它会将 null 识别为 object 类型
- 所以在使用 typeof 检测的时候, 不要用来检测 null 这个类型, 因为检测结果不准确
- 后续的时候会介绍一个方法能够用来检测 null 这个类型
- JS 问题
- 示例: JS 校验 null 的类型方法有哪些
数据类型转换
- JS 转换的时候 任意类型都能够转换为数字使用, 主要是字符串转数字
-
- 借助一个 转型函数 Number(数据)
-
- parseInt(数据)
-
- parseFloat(数据) 参考 上述的 parseInt 自己整理练习
-
- 开发中我最喜欢的一个 方式变量/数据 - 0
var box = '10086'
console.log(box)
console.log(box - 0)
var box = '10086'
console.log('box 原本的数据类型', typeof(box), box)
var newBox = Number(box)
console.log('box 通过转型函数转换后的值', typeof(newBox), newBox)
console.log(Number(''))
console.log(Number(' '))
console.log(Number('qwertyui'))
console.log(Number("123456789"))
console.log(Number(true))
console.log(Number(false))
console.log(Number(undefined))
console.log(Number(null))
console.log(parseInt(100))
console.log(parseInt(100.666))
console.log(parseInt(''))
console.log(parseInt(' '))
console.log(parseInt('!@#$%^&'))
console.log(parseInt('qwerty'))
console.log(parseInt('qwerty10086'))
console.log(parseInt('10086qwerty'))
console.log(parseInt('10086qwe999rty'))
console.log(parseInt('100'))
console.log(parseInt('100.10086'))
转字符串类型
-
- 变量/数据.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())
请书写 false 的转换
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)))
console.log(typeof(undefined), typeof(undefined + ''))
console.log(typeof(null), typeof(null + ''))
转布尔类型
- 一般开发的时候不会主动的转换布尔类型
- 一般是隐式转换, 也就是由 JS 帮我们完成了数据的转换, 一般做判断的时候比较
- 1.借助一个转型函数 Boolean(变量/数据)
- 2.!!变量/数据
- 一个! 表示得到这个数据取反后的布尔值
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))
console.log(Boolean(''))
console.log(Boolean(' '))
console.log(Boolean('1234567'))
console.log(Boolean('!@#$'))
console.log(Boolean('qwerts'))
console.log(Boolean('你好!@#$%qwert12345'))
console.log(Boolean(undefined))
console.log(!undefined)
console.log(!!undefined)
JS 也叫 操作符, 是 JS 中 发起一个运算的最简单的方式
- 算数运算符: + - * / %(加 减 乘 除 取余)
- 运算符本身就是给 数字类型使用的
-
- 注意: + 一般是给数字使用的, 但是如果 符号的任意一边有字符串类型的, 那么不在计算求和, 而是计算一个拼接
-
- 运算的时候, 如果符号两边有字符串. 那么 JS 会将 字符串转换为 数字 然后参与运算这也是为什么 数据 - 0 能够转换为 number 类型
赋值运算符
- = 赋值号
- += 当要给一个变量重新赋值, 赋值为他本身加一个内容, 就可以使用 +=
var a = 100
a += 500
console.log(a)
var b = 100
b -= 50
console.log(b)
var c = 10
c *= 10
console.log(c)
var d = 9
d /= 3
console.log(d)
var q = 9
q %= 4
console.log(q)
比较运算符
- 大于 >
- 小于 <
- 大于等于 >=
- 小于等于 <=
- 等于 == 对比两边是否相等, 不会区分数据类型 了解或者特殊情况下
- === 对比两边是否相等, 区分数据类型 推荐写 ===
- 一定要注意, 等于 最少要两个 == 千万不要写成 =
- 不等于
- != 对比两边是否不相等, 不会区分数据类型
- !== 对比两边是否不相等, 区分数据类型 推荐写 !==
console.log(100 != 100)
console.log(100 != 99)
console.log(100 !== 100)
console.log(100 !== '100')
console.log(100 != '100')
需求:书写一段代码, 让 abc 保留两位小数, 值为 123.45
需求: 分析出 num 的运算过程以及结果
- var num = 3 - '36' % 5 + '2' - 2