Home > Article > Web Front-end > How many data types are there in JavaScript?
There are 9 data types in JavaScript, namely: String, Number, Boolean, Null, Undefined, Symbol, Array, Function, and Object (Object).
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Data type refers to the type of value that can be stored and manipulated in the program. Each programming language has its supported data types. Different data types are used to store different data, such as text and numerical values. , images, etc.
JavaScript is a dynamically typed language. When defining a variable, you do not need to specify the type of the variable in advance. The type of the variable is dynamically determined by the JavaScript engine during the running of the program. In addition, you can use the same Variables to store different types of data, for example:
var a; // 此时 a 为 Undefined a = "http://c.biancheng.net/"; // 此时 a 为 String 类型 a = 123; // 此时 a 为 Number 类型
Data types in JavaScript can be divided into two types:
Basic data types (value types): characters String, Number, Boolean, Null, Undefined, Symbol;
Reference data type: Object, array (Array), function (Function).
Tip: Symbol is a new data type introduced in ECMAScript6 that represents a unique value.
1) String type
The string (String) type is a piece of text wrapped in single quotes '' or double quotes "", such as ' 123',"abc". It should be noted that single quotes and double quotes are different ways of defining a string and are not part of the string.
2) Number type
Number type is used to define numerical values. JavaScript does not distinguish between integers and decimals (floating point numbers), and the Number type is used uniformly
Note: The number of values that can be defined by the Number type is not unlimited. The Number type in JavaScript can only represent values between -(2∧53 - 1) and (2∧53 -1).
3) Boolean type
The Boolean type has only two values, true (true) or false (false), which are used for comparison when making conditional judgments. Many, in addition to directly using true or false to define Boolean type variables, you can also use some expressions to get Boolean type values
4) Null type
Null is a special data type with only one value, which represents a "null" value, that is, there is no value, nothing. It is used to define a null object pointer.
Use the typeof operator to check the type of Null. You will find that the type of Null is Object, which means that Null actually uses a special value belonging to Object. So by assigning the variable to Null we create an empty object.
5) Undefined type
Undefined is also a special data type with only one value, which means undefined. When we declare a variable but do not assign a value to the variable, the default value of the variable is Undefined.
6) Symbol type
Symbol is a new data type introduced in ECMAScript6, which represents a unique value. Symbol type values need to use the Symbol() function To generate
7) Array type
An array (Array) is a collection of data arranged in order. Each value in the array is called an element. And the array can contain any type of data. To define an array in JavaScript, you need to use square brackets [ ]
. Each element in the array is separated by commas, for example:
[1, 2, 3, 'hello', true, null]
In addition, you can also use the Array() function to create an array. , as shown in the following example:
var arr = new Array(1, 2, 3, 4); console.log(arr); // 输出 [1, 2, 3, 4]
8) Function type
A function (Function) is a code block with a specific function. The function does not run automatically. It needs It can be run by calling the function name, as shown in the following example:
function sayHello(name){ return "Hello, " + name; } var res = sayHello("Peter"); console.log(res); // 输出 Hello, Peter
In addition, functions can also be stored in variables, objects, and arrays, and functions can also be passed as parameters to other functions, or returned from other functions .
9) Object 类型
JavaScript 中的对象(Object)类型是一组由键、值组成的无序集合,定义对象类型需要使用花括号{ },语法格式如下:
{name1: value1, name2: value2, name3: value3, ..., nameN: valueN}
其中 name1、name2、name3、...、nameN 为对象中的键,value1、value2、value3、...、valueN 为对应的值。
在 JavaScript 中,对象类型的键都是字符串类型的,值则可以是任意数据类型。要获取对象中的某个值,可以使用对象名.键
的形式。
【推荐学习:javascript高级教程】
The above is the detailed content of How many data types are there in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!