Home >Web Front-end >Front-end Q&A >How is JavaScript data divided?
Javascript is a programming language widely used in web development. In the Javascript language specification, data types are a very important part. Therefore, this article will introduce the division of Javascript data types to help readers better understand Javascript.
Javascript data types are divided into two types: primitive types and reference types.
There are six primitive types of Javascript: Boolean, Null, Undefined, Number, String and Symbol. Their characteristic is that they are stored in stack memory and do not occupy heap memory.
The Boolean type has only two values: true and false. Generally used for logical judgment, Boolean operations, etc.
let isTrue = true; let isFalse = false;
The Null type represents a null value. If the value of a variable is null, then it represents a null object pointer.
let myNull = null;
The Undefined type represents an undeclared variable or the value of the variable is not assigned. When a variable has not been initialized or has no return value, its value is undefined.
let myUndefined; console.log(myUndefined); // undefined
TheNumber type is used to represent numbers. It can be an integer or a decimal, or it can be expressed in scientific notation.
let myAge = 30; let myPi = 3.1415926; let myMoney = 10e6; //科学计数法,等同于 10000000
String type is used to represent a string, that is, a series of characters.
let myName = "Tom"; let myIntro = "I am a developer";
The Symbol type represents a unique identifier. Each Symbol value is unique. Symbol is usually used as an identifier for object properties.
let mySymbol = Symbol("some symbol"); console.log(mySymbol); // Symbol(some symbol)
Reference type is the collective name for complex data types in Javascript, including objects, arrays, functions, etc. They are characterized by being stored in heap memory.
The Object type is one of the most basic data types in Javascript. It is used to represent an unordered set of key-value pairs. Object's keys must be of string or symbol type.
let myObject = { name: "Tom", age: 30, address: "New York" };
The Array type is used to represent an ordered collection of elements. It is often used to store a set of data.
let myArray = [1, 2, 3, 4, 5];
Function type is used to create a function object. A function object contains a sequence of executable blocks of statements.
function add(a, b) { return a + b; }
Date type is used to represent a date and time.
let currentDate = new Date();
The RegExp type is used to represent a regular expression.
let myRegexp = /ab+c/;
In addition, there are many other reference types, such as Map, Set, Promise, etc. They are also widely used in Javascript programming.
This article introduces the division of Javascript data types into primitive types and reference types. The primitive types include: Boolean, Null, Undefined, Number, String and Symbol. Reference types include: Object, Array, Function, Date, RegExp, etc. Understanding the data type classification of Javascript will help you understand and use the language specifications of Javascript, and will help your Javascript programming skills.
The above is the detailed content of How is JavaScript data divided?. For more information, please follow other related articles on the PHP Chinese website!