分类
- 基本数据类型
- 引用数据类型
基本数据类型
String
字符串Number
数字Boolean
布尔Null
: 没有Undefined
: 未定义symbol
: 表示独一无二的值
代码块中的例子用的时候 注释一下前面使用过的例子
String 字符串(常用方法)
let type = typeof "text"
console.log(type) // string
// 获取字符串长度 length
let str = "这是一个字符串"
let strLen = str.length
console.log(strLen) // 7
// 拆分字符串 split
// 不改变原字符串
let str = "这是一个字符串"
let splitArr = str.split("") // 以空格为分隔 拆分成数组
console.log(splitArr) // ["这", "是", "一", "个", "字", "符", "串"]
let str2 = "这是,一个,字符串"
let splitArr2 = str2.split(",") // 以逗号为分隔 拆分成数组
console.log(splitArr2) // ["这是", "一个", "字符串"]
// 获取字符串索引 indexOf lastIndexOf
let str = "这是一个字符串"
let strIndex1 = str.indexOf("一") // 获取目标字符串 一 在字符串str中的索引位置 从前往后查找
let strIndex2 = str.lastIndexOf("一") // 从后往前查找
console.log(strIndex1) // 2
console.log(strIndex2) // 2
// 是否包含某段字符串 includes
let str = "这是一个字符串"
let hasStr = str.includes("一个") // 是否包含文本 一个 返回 true/false
console.log(hasStr) // true
// 拼接字符串 concat
// 不改变原字符串
let str1 = "这是一个"
let str2 = "字符串"
let target1 = str1.concat(str2)
let target2 = "".concat(str1, str2)
console.log(target1) // 这是一个字符串
console.log(target2) // 这是一个字符串
// 替换字符串 replace
// 不改变原字符串 返回新的替换后的字符串
let str = "这是一个字符串"
let str2 = str.replace("个", "串") // 只匹配第一个 个 具体的可以百度下
console.log(str) // 这是一个字符串
console.log(str2) // 这是一串字符串
let str = "这是一个个字符串"
let reg = /个/g // 通过正则匹配所有的个
let str2 = str.replace(reg, "串")
console.log(str) // 这是一个个字符串
console.log(str2) // 这是一串串字符串
// 转换成大写 toUpperCase
let str = "abc"
let str2 = str.toUpperCase()
console.log(str) // abc
console.log(str2) // ABC
// 转换成小写 toLowerCase
let str = "ABC"
let str2 = str.toLowerCase()
console.log(str) // ABC
console.log(str2) // abc
// 去除前后空格 trim
let str = " 这是一个 "
let str2 = str.trim()
console.log(str) // " 这是一个 "
console.log(str2) // "这是一个"
// 提取某段字符串
let str = "这是一个字符串"
let str2 = str.slice(0, 2) // 从索引0 开始 截取2个
console.log(str) // "这是一个字符串"
console.log(str2) // "这是"
上面为常用的字符串方法,要看更多的可以点击字符串所有方法
Number 数字
let num = 123
// 实际情况中 可能碰到的想要的是number类型 但是拿到的是string
// 可以通过 Number() 转换成数字
let num = "123"
num = Number(num)
// 还可以通过~~ 对确定是整数的数使用 相当于Math.floor()
let num = "123"
num = ~~num
// 数字取整操作 数字 或者 字符串数字
parseInt(a,10);
Math.floor(a);
a>>0;
~~a;
a|0;
// 将字符串转换成浮点数 Number.parseFloat()
// 将字符串转换成整型数字 Number.parseInt()
// Number.isFinite() 判断传递的参数是否为有限数字。
// Number.isInteger() 判断传递的参数是否为整数。
// Number.isNaN() 判断传递的参数是否为 isNaN()。
// Number.isSafeInteger() 判断传递的参数是否为安全整数。
// toFixed()
let num = 2
num.toFixed(2) // 2.00
num.toFixed(3) // 2.000
Math方法(常用)
// 取随机数 Math.random() (包括0,不包括1)
// 取范围 (min,max) 之间的随机数
// Math.random() * (max - min) + min
Math.random() * (5 - 0) + 0 // (0, 5)
Math.random() * (5 - (-5)) + (-5) // (-5, 5)
Math.random() * (8 - 6) + 6 // (6, 8)
// 向上取整数 Math.ceil
let num = 1.2
let ceilNum = Math.ceil(num)
console.log(ceilNum) // 2
// 向下取整数 Math.floor
let num = 1.2
let ceilNum = Math.floor(num)
console.log(ceilNum) // 1
// 四舍五入 Math.round
let num = 1.2
let ceilNum = Math.round(num)
console.log(ceilNum) // 1
let num = 1.6
let ceilNum = Math.round(num)
console.log(ceilNum) // 2
// π Math.PI
console.log(Math.PI) // 3.141592653589793
// 取绝对值 Math.abs(x)
let num = -2
Math.abs(num) // 2
上面是个人Math实战中常用的,更多请点击Math所有方法