js型別轉換

2021-05-22 01:39:03 字數 4359 閱讀 8713

js**

varscolor = "blue"

;   

alert(scolor.length);//outputs "4"

js**

varbfound =false;   

alert(bfound.tostring());//outputs "false"

js**

varinum1 = 10;   

varfnum2 = 10.0;   

alert(fnum2.tostring()); //outputs "10"

js**

varinum = 10;   

alert(inum.tostring(16)); //outputs "a"

js**

varinum1 = parseint("1234blue"

varinum2 = parseint("oxa"

varinum3 = parseint("22.5"

varinum4 = parseint("blue"

); //returns nan

js**

varinum1 = parseint("af"

varinum2 = parseint("10"

varinum3 = parseint("10"

varinum4 = parseint("10"

,10); //returns 10

js**

varinum1 = parseint("010"

varinum2 = parseint("010"

varinum3 = parseint("010"

,10);//returns 10

js**

varfnum1 = parsefloat("1234blue"

varfnum2 = parsefloat("0xa"

varfnum3 = parsefloat("22.5"

varfnum4 = parsefloat("22.34.5"

varfnum5 = parsefloat("0908"

varfnum6 = parsefloat("blue"

);//returns nan

js**

varb1 = boolean(""

varb2 = boolean("hi"

var

varb4 = boolean(null

varvarb6 = boolean(newobject());//true

js**

number(false

number(true

number(null

number("5.5"

number("56"

number("5.6.7"

number(new

number(100);//100

js**

vars1 = string(null

varonull =null;   

vars2 = onull.tostring();//causes an error

(2).prototype----對該物件的物件原型的引用。對於所有的類,它預設返回object物件的乙個例項。 js

** varofalseobject   =newboolean(false);   

varbresult = ofalseobject   &&true;//outputs   true;

js**

varonumberobject =newnumber(99);   

aler(onumberobject.tofixed(2));//outputs "99.00"

js**

varonumberobj =newnumber(99);   

alert(onumberobj.toexponential(1));//outputs "9.9e+1"

js**

varonumberobj =newnumber(99);   

alert(onumberobj.toprecision(1));//outputs "1e+2" ==100

js**

varonumberobj =newnumber(99);   

alert(onumberobj.toprecision(3));// outputs "99.0"

js**

alert(ostringobj.valueof() == ostringobj.tostring());//outputs "true"

js**

varostringobj =newstring("hello world"

);   

alert(ostringobj.length);outputs "11"

js**

varostringobj =newstring("hello world"

);   

alert(ostringobj.charat(1));outputs "e"

js**

varostringobj =newstring("hello world"

);   

alert(ostringobj.charcodeat(1));outputs "101"

js**

varostringobj =newstring("hello "

);   

varsresult = ostringobj.concat("world"

alert(ostringobj);//outputs "hello"

js**

varostringobj =newstring("hello world"

);   

alert(ostringobj.slice(3,7));//outputs "lo w"

js**

varostringobj =newstring("hello world"

);   

alert(ostringobj.substring(3,-4));//outputs "hel" substring()總是把較小的數字作為起始位,較大的數字作為終止位。

tolowercase()、tolocallowercase()、touppercase()和tolocaleuppercase(): js

** varostrobj =newstring("hello world"

);   

alert(ostrobjinstanceofstring);//outputs "true"

JS 型別轉換 強制型別轉換

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

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...

js型別轉換

1 整數和字元相互轉換 js提供了parseint 和parsefloat 兩個轉換函式。前者把值轉換成整數,後者把值轉換成浮點數。只有對string型別呼叫這些方法,這兩個函式才能正確執行 對其他型別返回的都是nan not a number 在判斷字串是否是數字值前,parseint 和pars...