Home  >  Article  >  Web Front-end  >  How is JavaScript data divided?

How is JavaScript data divided?

PHPz
PHPzOriginal
2023-04-24 15:51:10712browse

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.

Primitive 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.

Boolean type

The Boolean type has only two values: true and false. Generally used for logical judgment, Boolean operations, etc.

let isTrue = true;
let isFalse = false;

Null type

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;

Undefined type

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

Number type

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

String type is used to represent a string, that is, a series of characters.

let myName = "Tom";
let myIntro = "I am a developer";

Symbol type

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

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.

Object type

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"
};

Array type

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

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

Date type is used to represent a date and time.

let currentDate = new Date();

RegExp type

The RegExp type is used to represent a regular expression.

let myRegexp = /ab+c/;

Other reference types

In addition, there are many other reference types, such as Map, Set, Promise, etc. They are also widely used in Javascript programming.

Summary

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!

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