Home>Article>Web Front-end> What are the commonly used data types in JavaScript?

What are the commonly used data types in JavaScript?

青灯夜游
青灯夜游 Original
2021-06-28 16:18:54 20086browse

Commonly used data types in JavaScript are: 1. null type, which represents a null value and defines a null object pointer; 2. undefined type, which represents an undefined value; 3. number type; 4. string type; 5 , boolean type; 6. object type; 7. Array type.

What are the commonly used data types in JavaScript?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Commonly used data types in javascript

Data types Description
null Null value means non-object
undefined Undefined value means unassigned initialization value
number Number, the value of mathematical operation
string String, representing the flow of information
boolean Boolean value, the value of logical operation
object Object, representing a data set of a composite structure
Array Array, containing an ordered collection of encoded values.

1. Null

The Null type has only one value, null, which represents a null value and defines a null object pointer.

Use the typeof operator to detect null values and return Object, indicating that it belongs to the object type, but JavaScript classifies it as a special type of value.

Set the initialization value of the variable to null, and you can define a spare empty object, which is a special object value, or a non-object. For example, if you detect that an object is empty, you can initialize it.

if (men == null){ men = { //初始化men } }

2. Undefined type

undefined is the only value of the Undefined type, which represents an undefined value. When a variable is declared without a value assigned, or a property is defined without a value set, the default value is undefined.

Example 1

undefined is derived from null. Both null and undefined represent empty values. When converted into Boolean values, they are both false values and can be equal.

console.log(null == undefined); //返回 true

null and undefined belong to two different types, which can be detected using the equality operator (==) or the typeof operator.

console.log(null === undefined); //false console.log(typeof null); //返回"object" console.log(typeof undefined); //返回"undefined"

Example 2

To check whether a variable is initialized, you can use undefined to quickly detect.

var a; //声明变量 console.log(a); //返回变量默认值为 undefined (a == undefined) && (a = 0); //检测变量是否初始化,否则为其赋值 console.log(a); //返回初始值 0

You can also use the typeof operator to detect whether the type of a variable is undefined.

(typeof a == "undefined") && (a = 0); //检测变量是否初始化,否则为其赋值

3. Boolean type

Boolean type (Boolean) contains only two fixed values: true and false. Among them, true represents "true" and false represents "false".

In JavaScript, the six special values of undefined, null, "", 0, NaN and false are false when converted to Boolean values, which are called false values. Except for false values, any other type of data converted to a Boolean value is true.

Example

Use the Boolean() function to force a value to a Boolean value.

console.log(Boolean(0)); //返回 false console.log(Boolean(NaN)); //返回 false console.log(Boolean(null)); //返回 false console.log(Boolean("")); //返回 false console.log(Boolean(undefined)); //返回 false

4. Number:

There are two forms of representation of this type, the first is an integer, and the second is a floating point number. Integer: can be represented by decimal, octal, and hexadecimal literal values. Floating point number: The value must contain a decimal point and there must be one digit after the decimal point.

5. String:

The String type is used to represent a character sequence consisting of zero or more 16-bit Unicode characters, that is, a string. As for whether to use single quotes or double quotes, there is still no difference in js. Remember to come in pairs.

6. Object:

The Object data type, called an object, is a collection of data and functions (functions). It can be created using the new operator followed by the name of the object type to be created. Can also be created using literal notation. Add a property with a different name (any string including the empty string).

7. Array

JavaScript arrays are written in square brackets. The items of the array are separated by commas.

The following code declares (creates) an array named cars, containing three items (car brands):

var cars = ["Porsche", "Volvo", "BMW"];

The array index is based on zero, which means that the first item is [ 0], the second item is [1], and so on.

Arrays in ECMAScript are quite different from arrays in other languages:

  • Each item in an ECMAScript array can save any type of data;

  • The size of the ECMAScript array can be dynamically adjusted, and elements can be added or deleted from the array;

[Related recommendations:javascript learning tutorial

The above is the detailed content of What are the commonly used data types in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn