English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript基础教程

JavaScript 对象

JavaScript 函数

JS HTML DOM

JS 浏览器BOM

AJAX 基础教程

JavaScript 参考手册

JavaScript Data Types

值类型(基本类型):字符串(String)、数字(Number)、布尔(Boolean)、对空(Null)、未定义(Undefined)、Symbol。 引用数据类型:对象(Object)、数组(Array)、函数(Function)。


数据类型用于以编程语言对一种特定类型的数据进行分类。

例如,数字和字符串是不同类型的数据,JavaScript将对其进行不同的处理。

JavaScript变量可以包含许多数据类型:数字,字符串,数组,对象等:

 var length = 4;// Number 数字
 var firstName = "Vishal";  // String 字符串
 var user = {firstName:"Vishal", age:22};  // Object 对象
 var fruits = ["Apple", "Mango", "Banana"];// Array 数组

JavaScript类型是动态的

JavaScript是一种松散类型或动态语言。JavaScript中的变量并不直接与任何特定的值类型相关联,并且可以为所有变量分配(并重新分配)所有类型的值。

var x = 20; // x 是一个数字
x = "VISHAL";   // x 是一个字符串
x = true;   // x 是一个布尔值
Test to see‹/›

JavaScript 数字

JavaScript只有一种数字类型,没有整数和浮点数的单独指定。

因此,数字可以用JavaScript编写,带或不带小数。

var a = 25; // 没有小数
var b = 80.05;  // 右小数
Test to see‹/›

在以上两种情况下,数据类型都是一个数字,并且无论该数字是否带有小数点都相同。

可以在JavaScript中使用科学的指数表示法来缩写非常大或很小的数字,如以下示例所示:

let num1 = 957e8; // 95700000000
let num2 = 957e-8;// 0.00000957
Test to see‹/›

您将在本教程的后面部分进一步了解数字类型。

JavaScript 字符串

字符串是一个或多个字符(字母,数字,符号)的序列。字符串很有用,因为它们代表文本数据。

JavaScript字符串用于存储和处理文本。

与某些其他语言不同,JavaScript在单引号和双引号之间没有区别。

var msg1 = "Hello world";   // 使用双引号
var msg2 = 'Hello world';   // 使用单引号
Test to see‹/›

您可以在字符串内使用引号,只要它们与字符串周围的引号不匹配即可:

var str1 = 'She said "Hey" and left';  // 双引号内的单引号
var str2 = "She said 'Hey' and left";  // 单引号内的双引号
var str3 = "Let's have a cup of tea";  // 双引号内的单引号
var str4 = 'We\'ll never give up'; // 用反斜杠转义单引号
Test to see‹/›

您将在本教程的后面部分了解有关字符串的更多信息。

JavaScript 布尔值

布尔数据类型可以是两个值之一,即 true false.

在条件测试中经常使用布尔值。

(5 > 10)  // 返回false
(5 < 10)  // 返回true
("Apple" === "Apple") // 返回true
Test to see‹/›

您将在本教程的后面部分了解有关布尔值的更多信息。

JavaScript 数组

数组用于将多个值存储在单个变量中。

JavaScript数组用方括号括起来[]。

数组项用逗号分隔。

以下代码声明(创建)一个名为fruits的数组,其中包含三个项目(三种水果名称):

var fruits = ["Apple", "Mango", "Banana"];
Test to see‹/›

数组内部的每个项目或值都称为一个元素。您可以通过使用索引号来引用数组的元素。

JavaScript数组的索引为零:数组的第一个元素的索引为0,第二个元素的索引为1,依此类推。

您将在本教程的后面部分了解有关数组的更多信息。

JavaScript 对象

在JavaScript中,对象可以看作是属性的集合。

JavaScript对象用花括号括起来{}。

对象属性写为名称:值对,以逗号分隔。

var user = {firstName:"Vishal", age:22, color: "blue", location: "unknown"};
Test to see‹/›

The object (user) in the above example has4properties: firstName, age, color, and location.

You will learn more about objects in the later part of this tutorial.

The type of the operator

The typeof operator can help you find the type of your variables.

The typeof operator returns the type of a variable or expression.

typeof ""  // Returns "string"
typeof "Vishal"// Returns "string"
typeof ""42"// Returns "string" (Number within quotes is String)
typeof 42  // Returns "number"
typeof true// Returns "boolean"
typeof false   // Returns "boolean"
typeof undefined   // Returns "undefined"
typeof null// Returns "object"
typeof {name: "Vishal", age:22} // Returns "object"
typeof [2, 4, 6, 8]// Returns "object" (not "array", see note below)
typeof function myFunc(){} // Returns "function"
Test to see‹/›

Note:The typeof operator returns "object" for arrays because arrays are objects in JavaScript.

undefined (undeclared)

undefinedThe value indicates that no value has been assigned to the variable, or the variable has not been declared at all.

var city;// Value is undefined
Test to see‹/›

The type is alsoundefined.

typeof city   // "undefined"
Test to see‹/›

By setting the value to, you can clear any variableundefined. The type will also beundefined.

city = undefined;   // Value is undefined, type is undefined
Test to see‹/›

null (empty value)

The valuenullrepresents the intentional absence of any object value.

In JavaScript, the data typenullis an object.

By setting the value to, you can clear any objectnull.

var user = {firstName: "Vishal", age:"22", color: "blue"};
user = null;// Now value is null, but the data type is still an object
Test to see‹/›

You can also clear an object by setting it toundefined.

var user = {firstName: "Vishal", age:"22", color: "blue"};
user = undefined;   // Both value and type are undefined
Test to see‹/›

The difference between null and undefined

nullwithundefinedValues are equal but of different types.

typeof null  // "object" (not "null" for legacy reasons)
typeof undefined // "undefined"
Test to see‹/›

When checkingnullorundefinedPlease note the difference between the equality operator (==) and the identity operator (===), because the former performs type conversion.

null == undefined // true
null === undefined// false

Handle multiple data types simultaneously

Although each program you create will contain multiple data types.

When using operators that cross data types (such as+Unexpected results may occur when using operators that can add numbers or concatenate strings.

When adding numbers and strings, JavaScript will treat numbers as strings.

var x = 5 + "Sky";
Test to see‹/›

When adding strings and numbers, JavaScript will treat numbers as strings.

var x = "Sky" + 5;
Test to see‹/›

JavaScript calculates expressions from left to right. Different sequences can produce different results:

var x = 5 + 3 + "Sky";
Test to see‹/›
var x = "Sky" + 5 + 3;
Test to see‹/›

In the first example, JavaScript will5And3Treated as numbers until reaching 'Sky'.

In the second example, since the first operand is a string, all operands are treated as strings.