Js--各种基本类型之间的转换

  • 数字转字符串
let num = 24
let str1 = num.toString() 
let str2 = num + ''
  • 字符串转数字
let s = '24'
let num1 = Number(s) 
let num2 = s - 0
let num3 = parseInt(s)
  • 数字转数字数组
let num = 24
let numArr = num.toString().split('').map(Number)
  • 数字转字符串数组
let num = 24
let strignArr = num.toString().split('')
  • 数字数组或字符串数组转数字
let numArr1 = [1,8,24]
let numArr2 = ['1','8','24']
let num1 = numArr1.join('') - 0 
let num2 = Number(numArr2.join(''))
let num3 = parseInt(numArr1.join(''))
  • 数字数组转字符串数组
let numArr = [1,8,24]
let strArr = numArr.map(String)

  • 字符串转数组
let str='Franklin'
let arr1 = str.split('')
let arr2 = Array.from(str)

你可能感兴趣的:(js)