Home >Web Front-end >Front-end Q&A >What are the basic data types in javascript

What are the basic data types in javascript

青灯夜游
青灯夜游Original
2021-07-19 16:26:257587browse

The basic data types of JavaScript are: null (null value), undefined (undefined value), number (number), string (string), boolean (Boolean value), object (object).

What are the basic data types in javascript

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

JavaScript defines 6 basic data types, as shown in the table:

Basic data types of JavaScript
Data type Description
null Null value means non-object
undefined Undefined value, indicating an unassigned initialization value
number Number, the value of mathematical operation
string String, representing information flow
boolean Boolean value, the value of logical operation
object Object, representing a data set with a composite structure

You can use the typeof operator to detect the basic structure of the data type.

Example 1

The following code uses the typeof operator to detect the types of common values.

console.log(typeof 1);  //返回字符串"number"
console.log(typeof "1");  //返回字符串"string"
console.log(typeof true);  //返回字符串"boolean"
console.log(typeof {});  //返回字符串"object"
console.log(typeof []);  //返回字符串"object"
console.log(typeof function(){});  //返回字符串"function"
console.log(typeof null);  //返回字符串"object"
console.log(typeof undefined) ;  //返回字符串"undefined"

Note:

The typeof operator returns one of the six basic types in the form of a string. However, through comparison, it can be found that there are two differences between the typeof return value and the above table. Brief explanation As follows:

Classify null as the Object type, rather than as a value of a special type (Null).

Classify function(,){} as Function type. That is, the function is regarded as an independent basic data type, rather than a special subclass of the Object type.

Example 2

Since the return type of null value is Object, using the following custom function can avoid the impact of null value on basic type detection.

//如果是 null 值,则先返回字符串 "null" 否则返回(typeof o)的值
function typeOf(o){
    return (o === null) ? "null" : (typeof o);
}
console.log(typeOf(1));  //返回字符串"number"
console.log(typeOf("1"));  //返回字符串"string"
console.log(typeOf(true));  //返回字符串 "boolean"
console.log(typeOf({}));  //返回字符串"object"
console.log(typeOf(null));  //返回字符串"null"
console.log(typeOf(undefined));  //返回字符串"undefined"

In JavaScript, a function is a special structure. It can be a collection of code, or a data type; it can be used as an object, or it can be used as a constructor to create a type. The usage of JavaScript functions is relatively flexible, which is also a manifestation of the agility of the JavaScript language (functional programming).

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of What are the basic 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