JavaScript 中的數據類型有兩類:原始類型和對象。原始類型包括:
* 字符串
* 數字
* 布爾
* null
* undefined
## 字符串
字符串是被單引號或雙引號包含的文本。最佳實踐是始終保持使用一種引號。有時候字符串里的引號標記會和創建字符串的引號沖突,在這種情況下,可以使用`\`?反斜線轉義字符或者使用不同的引號。
~~~
// Strings can be created with double or single quotes.
var a = "I am a string";
var b = 'So am I!';
alert( a );
alert( b );
~~~
~~~
// Sometimes a string may contain quotation marks.
var statement1 = 'He said "JavaScript is awesome!"';
var statement2 = "He said \"JavaScript is awesome!\"";
~~~
## 數字
數字是任何正或負的數值。整數和浮點數之間沒有區別。
~~~
// Numbers are any whole or floating point integer.
var num1 = 100;
var num2 = 100.10;
var num3 = 0.10;
~~~
## 布爾
布爾類型是?`true`?或者?`false`。
~~~
// Boolean values.
var okay = true;
var fail = false;
~~~
## null 和 undefined
`null`?和?`undefined`?是 JavaScript 中的特殊類型。 Null 類型表示一個空值,類似于許多其他語言。 Undefined 類型表示變量還沒有賦值的狀態,可以通過兩種方式來創建: 通過使用?`undefined`?關鍵字或者在定義變量的時候不賦值。
~~~
// Define a null value.
var foo = null;
// Two ways to achieve an undefined value.
var bar1 = undefined;
var bar2;
~~~
## 對象
其他一切都是對象。JavaScript 有眾多的?[內置對象](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects "MDN - 內置對象"),但本指南只包括:
* 對象
* 數組
* 函數
最簡單的創建對象的方法是被稱為對象字面量的簡寫語法。這些簡單的對象是無序的鍵值對集合。對象中的鍵通常被稱為“屬性”,屬性的值可以是任何有效的 JavaScript 類型,甚至可以是另一個對象。創建或訪問對象的屬性,我們可以使用“點號表示法”或者“括號表示法”。
~~~
// Using an empty object literal
var person1 = {};
// Assign properties using "dot notation"
person1.firstName = "John";
person1.lastName = "Doe";
// Access properties using "dot notation"
alert( person1.firstName + " " + person1.lastName );
// Creating an object with the object literal syntax:
var person2 = {
firstName: "Jane",
lastName: "Doe"
};
alert( person2.firstName + " " + person2.lastName );
var people = {};
// Assign properties using "bracket notation"
// As mentioned, objects can also have objects as a property value
people[ "person1" ] = person1;
people[ "person2" ] = person2;
// Access properties using a mix of both bracket and dot notation
alert( people[ "person1" ].firstName );
alert( people[ "person2" ].firstName );
~~~
如果被訪問的屬性還未定義,那么它的值將是?`undefined`。
~~~
// Properties that have not been created are undefined.
var person = { name: "John Doe" };
alert( person.email ); // undefined
~~~
在?[對象](http://js101.co/javascript-101/objects.html)?部分會進一步討論 JavaScript 對象。
## 數組
數組是一類由它所包含的每一個項的索引排序的對象。索引開始于零,并擴展到已添加的項的數目,(項的數目)也是被稱為?`.length`?的數組屬性。類似一個基本對象,數組可以使用?`Array`?構造函數或者被稱為數組字面量的簡寫語法來創建。
~~~
// Creating an array with the constructor:
var foo = new Array;
// Creating an array with the array literal syntax:
var bar = [];
~~~
在這兩種語法之間有一個重要的區別。數組構造函數和數組字面量都可以在創建時加入要包含到數組的項,但是如果只是傳入一個單一的數字項,數組構造函數會將該數字項當作數組的長度值。
~~~
// The array literal returns a foo.length value of 1:
var foo = [ 100 ];
alert( foo[ 0 ] ); // 100
alert( foo.length ); // 1
// The array constructor returns a bar.length value of 100:
var bar = new Array( 100 );
alert( bar[ 0 ] ); // undefined
alert( bar.length ); // 100
~~~
數組可以通過已經存在數組實例中的方法來進行相關操作。數組中的項可以通過括號和給定的索引來引用。如果索引不存在或者不包括任何值,則返回值`undefined`。
一些常見的數組方法如下所示:
~~~
// Using the push(), pop(), unshift() and shift() methods on an array.
var foo = [];
foo.push( "a" );
foo.push( "b" );
alert( foo[ 0 ] ); // a
alert( foo[ 1 ] ); // b
alert( foo.length ); // 2
foo.pop();
alert( foo[ 0 ] ); // a
alert( foo[ 1 ] ); // undefined
alert( foo.length ); // 1
foo.unshift( "z" );
alert( foo[ 0 ] ); // z
alert( foo[ 1 ] ); // a
alert( foo.length ); // 2
foo.shift();
alert( foo[ 0 ] ); // a
alert( foo[ 1 ] ); // undefined
alert( foo.length ); // 1
~~~
還有更多的方法來操作數組,一部分將在[數組](http://js101.co/javascript-101/arrays.html)?進一步討論。更多的詳細信息可在[Mozilla 開發者網絡](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array "MDN - 數組參考")?找到。
## jQuery 中的類型檢測
jQuery 提供了一些基本的實用方法,用于判斷一個特定值的類型。類型檢測會在[類型檢測](http://js101.co/javascript-101/testing-type.html)部分進一步的討論,這里有一些例子:
~~~
// Checking the type of an arbitrary value.
var myValue = [ 1, 2, 3 ];
// Using JavaScript's typeof operator to test for primitive types:
typeof myValue === "string"; // false
typeof myValue === "number"; // false
typeof myValue === "undefined"; // false
typeof myValue === "boolean"; // false
// Using strict equality operator to check for null:
myValue === null; // false
// Using jQuery's methods to check for non-primitive types:
jQuery.isFunction( myValue ); // false
jQuery.isPlainObject( myValue ); // false
jQuery.isArray( myValue ); // true
~~~