How is JavaScript data divided?

PHPz
Release: 2023-04-24 16:05:00
Original
713 people have browsed it

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;
Copy after login

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;
Copy after login

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
Copy after login

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
Copy after login

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";
Copy after login

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)
Copy after login

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"
};
Copy after login

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];
Copy after login

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;
}
Copy after login

Date type

Date type is used to represent a date and time.

let currentDate = new Date();
Copy after login

RegExp type

The RegExp type is used to represent a regular expression.

let myRegexp = /ab+c/;
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template