js 型別轉換

2021-10-17 03:14:06 字數 757 閱讀 6508

//型別轉換的使用技巧

const number =99;

const string =

'99'

;const int =

"99asdfa"

;const float =

"99.123asdfa"

; console.

log(string*1)

;//字串轉數值

console.

log(

number

(string));

//字串轉數值

console.

log(

string

(number));

//數值轉字串

console.

log(number+"")

;//數值轉字串

console.

log(

parsefloat

(float));

//提取字串中前面的浮點型數值

console.

log(

parseint

(int));

//提取字串中前面的整型數值

console.

log(float.

split(""

));//字串轉陣列

console.

log(array.

join(""

));//陣列轉字串

JS 型別轉換 強制型別轉換

js 型別轉換 強制型別轉換 一 parseint 將其他型別轉換成number型別的整數 注意 string number 1 字串中值的首字元必須是數字,轉換後才能得到具體的值 否則得到nan 2 只轉換從首字元開始的連續數字 如 2b345 轉換後只得到2 3 除了string,其他型別轉換後...

js型別轉換

js varscolor blue alert scolor.length outputs 4 js varbfound false alert bfound.tostring outputs false js varinum1 10 varfnum2 10.0 alert fnum2.tostri...

js 型別轉換

function str function tostr val function todstr val,dv function trim str function lenb str function ismobile str d 13 d 15 d if reg.test trim str retu...