深入解析JavaScript中的數字對象與字串對象,javascript字串

來源:互聯網
上載者:User

深入解析JavaScript中的數字對象與字串對象,javascript字串

JavaScript Number 對象
JavaScript 只有一種數字類型。
可以使用也可以不使用小數點來書寫數字。
JavaScript 數字
JavaScript 數字可以使用也可以不使用小數點來書寫:
執行個體

var pi=3.14;  // 使用小數點var x=34;    // 不使用小數點


極大或極小的數字可通過科學(指數)計數法來寫:
執行個體

var y=123e5;  // 12300000var z=123e-5;  // 0.00123

所有 JavaScript 數字均為 64 位元
JavaScript 不是類型語言。與許多其他程式設計語言不同,JavaScript 不定義不同類型的數字,比如整數、短、長、浮點等等。
在JavaScript中,數字不分為整數類型和浮點型類型,所有的數字都是由 浮點型類型。JavaScript採用IEEE754標準定義的64位浮點格式表示數字,它能表示最大值為±1.7976931348623157 x 10308,最小值為±5 x 10 -324
值 (aka Fraction/Mantissa) 指數 Sign

52 bits (0 - 51) 11 bits (50 - 62) 1 bit (63)

精度
整數(不使用小數點或指數計數法)最多為 15 位。
小數的最大位元是 17,但是浮點運算並不總是 100% 準確:
執行個體

var x = 0.2+0.1; // result will be 0.30000000000000004


八進位和十六進位
如果首碼為 0,則 JavaScript 會把數值常量解釋為八位元,如果首碼為 0 和 "x",則解釋為十六進位數。
執行個體

var y = 0377; var z = 0xFF;


lamp 絕不要在數字前面寫零,除非您需要進行八進位轉換。
預設情況下,JavaScript 數字為十進位顯示。
但是你可以使用 toString() 方法 輸出16進位、8進位、2進位。
執行個體

var myNumber=128;myNumber.toString(16);  // returns 80myNumber.toString(8);  // returns 200myNumber.toString(2);  // returns 10000000


無窮大(Infinity)
當數字運算結果超過了JavaScript所能表示的數字上限(溢出),結果為一個特殊的無窮大(infinity)值,在JavaScript中以Infinity表示。同樣地,當負數的值超過了JavaScript所能表示的負數範圍,結果為負無窮大,在JavaScript中以-Infinity表示。無窮大值的行為特性和我們所期望的是一致的:基於它們的加、減、乘和除運算結果還是無窮大(當然還保留它們的加號或減號)。
執行個體

myNumber=2;while (myNumber!=Infinity){myNumber=myNumber*myNumber; // Calculate until Infinity}

除以0也產生了無限:
執行個體

var x = 2/0;var y = -2/0;


NaN - 非數字值
NaN 屬性是代表非數字值的特殊值。該屬性用於指示某個值不是數字。可以把 Number 對象設定為該值,來指示其不是數字值。
你可以使用 isNaN() 全域函數來判斷一個值是否是 NaN 值。
執行個體

var x = 1000 / "Apple";isNaN(x); // returns truevar y = 100 / "1000";isNaN(y); // returns false

除以0是無窮大,無窮大是一個數字:
執行個體

var x = 1000 / 0;isNaN(x); // returns false


數字可以是數字或者對象
數字可以私人資料進行初始化,就像 x = 123;
JavaScript 數字對象初始化資料, var y = new Number(123);
執行個體

var x = 123;var y = new Number(123);typeof(x) // returns Numbertypeof(y) // returns Object

執行個體

var x = 123;       var y = new Number(123);(x === y) // is false because x is a number and y is an object.

JavaScript 字串(String) 對象
String 對象用於處理已有的字元塊。
JavaScript 字串
一個字串用於儲存一系列字元就像 "John Doe".
一個字串可以使用單引號或雙引號:
執行個體

var carname="Volvo XC60";var carname='Volvo XC60';


你使用位置(索引)可以訪問字串中任何的字元:
執行個體

var character=carname[7];


字串的索引從零開始, 所以字串第一字元為 [0],第二個字元為 [1], 等等。
你可以在字串中使用引號,如下執行個體:
執行個體

var answer="It's alright";var answer="He is called 'Johnny'";var answer='He is called "Johnny"';


或者你可以在字串中使用逸出字元使用引號:
執行個體

var answer='It's alright';var answer="He is called "Johnny"";

字串(String)
字串(String)使用長度屬性length來計算字串的長度:
執行個體

var txt="Hello World!";document.write(txt.length);var txt="ABCDEFGHIJKLMNOPQRSTUVWXYZ";document.write(txt.length);

在字串中尋找字串
字串使用 indexOf() 來定位字串中某一個指定的字元首次出現的位置:
執行個體

var str="Hello world, welcome to the universe.";var n=str.indexOf("welcome");

如果沒找到對應的字元函數返回-1
lastIndexOf() 方法在字串末尾開始尋找字串出現的位置。
內容匹配
match()函數用來尋找字串中特定的字元,並且如果找到的話,則返回這個字元。
執行個體

var str="Hello world!";document.write(str.match("world") + "<br>");document.write(str.match("World") + "<br>");document.write(str.match("world!"));

替換內容
replace() 方法在字串中用某些字元替換另一些字元。
執行個體

str="Please visit Microsoft!"var n=str.replace("Microsoft","w3cschool");

字串大小寫轉換
字串大小寫轉換使用函數 toUpperCase() / toLowerCase():
執行個體

var txt="Hello World!";    // Stringvar txt1=txt.toUpperCase();  // txt1 is txt converted to uppervar txt2=txt.toLowerCase();  // txt2 is txt converted to lower

字串轉為數組
字串使用strong>split()函數轉為數組:
執行個體

txt="a,b,c,d,e"  // Stringtxt.split(",");  // Split on commastxt.split(" ");  // Split on spacestxt.split("|");  // Split on pipe 

特殊字元
Javascript 中可以使用反斜線(\)插入特殊符號,如:撇號,引號等其他特殊符號。
查看如下 JavaScript 代碼:

var txt="We are the so-called "Vikings" from the north.";document.write(txt);


在JavaScript中,字串的開始和停止使用單引號或雙引號。這意味著,上面的字串將被切成: We are the so-called
解決以上的問題可以使用反斜線來轉義引號:

var txt="We are the so-called \"Vikings\" from the north.";document.write(txt);


JavaScript將輸出正確的文本字串:We are the so-called "Vikings" from the north.
下表列出其他特殊字元,可以使用反斜線轉義特殊字元:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.