Table of Contents
The data types of JavaScript are divided into two types, one is the basic data type and the other is the reference data type
Home Web Front-end JS Tutorial Learn more about data types in JavaScript

Learn more about data types in JavaScript

Feb 17, 2021 am 09:01 AM
javascript type of data

Learn more about data types in JavaScript

The data types of JavaScript are divided into two types, one is the basic data type and the other is the reference data type

1. Basic data types include:

  • Number - - (number)

  • String - - (string)

  • Boolean - - (Boolean value)

  • Undefined - - (Undefined)

  • ##Null - - (Empty)

  • Symbol - - (Symbol)

2. Reference data types include:


1), Object - - (Object)

The following all belong to Object:

2.Array - - (array)
3.Function - - (function)
4.Date - - (time)
5.RegExp - - (regular)
6...(and many more)

Values ​​of basic data types:

Number:

Numbers (numbers can be with or without a decimal point), NaN, Infinity

String:

String Can be any text within quotes. Both double quotes and single quotes are OK. You can also use the ES6 template string ``

, such as:

var a = 'xxx';var a = "xxx";
Copy after login
, both of which are reputation strings.

Boolean:

There are only two values: true or false.

Boolean values ​​are commonly used in conditional testing. For example, to determine whether 1>2 is correct, return true if it is correct, and return false if it is incorrect. Then we can perform two different operations based on the returned results.

Undefined:

Take a value of undefined.

means the variable does not contain any value. is an undefined state.

Null:

indicates that the value of the variable is empty, and the variable can be cleared by setting the value of the variable to null.

Symbol:

For a detailed introduction to Symbol, please go to the detailed introduction of ES6 Ruan Yifeng symbols

Introduction to reference data types :

Array:

Array: Save a set of data

The function of the array object is : Use separate variable names to store a series of values.

(Dynamic array: the length can be automatically called based on the number of elements)

Concept:

1), Element: the data saved in the array space

2), length: the number of elements saved in the array
3), subscript (index): the number of the elements in the array, starting from 0 and ending with (length of the array - 1)

Usage:

a. Create array object:

var arr = [];//直接量
var arr = new Array();//创建数组对象。
var arr = [1,2,3];//直接量,在创建数组对象的同时初始化保存的数据。
var arr = new Array(1,2,3);在创建数组对象的同时初始化保存的数据。
var arr = new Array(size);//size为数字参数,表示创建数组时先预定size个空间。
Copy after login

b. Access array elements:

数组名[下标]
Copy after login

c , Array element traversal iteration:

    Ordinary loop
  • for(let i = 0;i < array.length; i++) {
    	//array[i]}
    Copy after login
    for-in
  • for(let 变量名 in 数组名) {
    	//变量名中所保存的值是数组下标编号的字符串内容
    	//仍然使用“数组名[字符串下标]”来访问数组对应下标处的元素}
    Copy after login
    for-of( ES6)
  • for(let 变量名 of 数组名) {
    	//变量名中所保存的值是数组中当前便利到的元素值}
    Copy after login
    Array api
  • For a detailed introduction to the array API, please see the common methods of the array

Object:

(OOP: Everything is an object)

The methods to create objects are:

1. Direct quantity:


var stu = {
	name: &#39;李四&#39;,
	age: 18,
	eat: function(pig) {
		console.log(&#39;吃:&#39; + pig)
	}}
Copy after login

2. Constructor creation


function Person() {
	this.name = &#39;jack&#39;;
	this.job = function() {
		alert(&#39;program&#39;);
	}}var person = new Person();
Copy after login

3. Create


var person = new Object();person.name = &#39;jack&#39;;person.sex = &#39;girl&#39;;
Copy after login

through object method. Attribute call of object:

Object name.Attribute name

Object name.Method name ([Parameter list])

Or:

Object name["Attribute name"]

Object name["Method name" 】();

Function:

A function is an event-driven or reusable block of code when it is called.

Essence: code block.

Definition:

1), declaration function


function 函数名(参数列表) {
	//函数主体:可被重复使用的代码块}
Copy after login

2), function expression


var 变量名 = function(参数列表) {
	//函数主体:可被重复使用的代码块}
Copy after login

3), understanding: new Function() //This is basically no longer needed, the above two are the abbreviations of this.


Call:

a. Direct call

函数名()
Copy after login

b. Event-driven


document.getElementById(‘xx’).onclick = 函数名;
Copy after login

parameters (equivalent to the input of a function ):

  • Formal parameters (formal parameters): parameters when the function is defined

  • Actual parameters (actual parameters): parameters when the function is called

Return value (equivalent to the output of the function):

return 表达式;
Copy after login

The function return value is returned to the function calling place

Basic data type The difference from reference data types:

The values ​​of basic data types are stored on the stack.

Values ​​of reference data types are stored in the heap.

For more programming-related knowledge, please visit:

Programming Teaching! !

The above is the detailed content of Learn more about data types in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What data type should be used for gender field in MySQL database? What data type should be used for gender field in MySQL database? Mar 14, 2024 pm 01:21 PM

In a MySQL database, gender fields can usually be stored using the ENUM type. ENUM is an enumeration type that allows us to select one as the value of a field from a set of predefined values. ENUM is a good choice when representing a fixed and limited option like gender. Let's look at a specific code example: Suppose we have a table called "users" that contains user information, including gender. Now we want to create a field for gender, we can design the table structure like this: CRE

What is the best data type for gender fields in MySQL? What is the best data type for gender fields in MySQL? Mar 15, 2024 am 10:24 AM

In MySQL, the most suitable data type for gender fields is the ENUM enumeration type. The ENUM enumeration type is a data type that allows the definition of a set of possible values. The gender field is suitable for using the ENUM type because gender usually only has two values, namely male and female. Next, I will use specific code examples to show how to create a gender field in MySQL and use the ENUM enumeration type to store gender information. The following are the steps: First, create a table named users in MySQL, including

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

What is the best data type choice for gender field in MySQL? What is the best data type choice for gender field in MySQL? Mar 14, 2024 pm 01:24 PM

When designing database tables, choosing the appropriate data type is very important for performance optimization and data storage efficiency. In the MySQL database, there is really no so-called best choice for the data type to store the gender field, because the gender field generally only has two values: male or female. But for efficiency and space saving, we can choose a suitable data type to store the gender field. In MySQL, the most commonly used data type to store gender fields is the enumeration type. An enumeration type is a data type that can limit the value of a field to a limited set.

Detailed explanation of how to use Boolean type in MySQL Detailed explanation of how to use Boolean type in MySQL Mar 15, 2024 am 11:45 AM

Detailed explanation of how to use Boolean types in MySQL MySQL is a commonly used relational database management system. In practical applications, it is often necessary to use Boolean types to represent logical true and false values. There are two representation methods of Boolean type in MySQL: TINYINT(1) and BOOL. This article will introduce in detail the use of Boolean types in MySQL, including the definition, assignment, query and modification of Boolean types, and explain it with specific code examples. 1. The Boolean type is defined in MySQL and can be

Introduction to basic syntax and data types of C language Introduction to basic syntax and data types of C language Mar 18, 2024 pm 04:03 PM

C language is a widely used computer programming language that is efficient, flexible and powerful. To be proficient in programming in C language, you first need to understand its basic syntax and data types. This article will introduce the basic syntax and data types of C language and give examples. 1. Basic syntax 1.1 Comments In C language, comments can be used to explain the code to facilitate understanding and maintenance. Comments can be divided into single-line comments and multi-line comments. //This is a single-line comment/*This is a multi-line comment*/1.2 Keyword C language

Python entry to proficiency: from zero basics to project development Python entry to proficiency: from zero basics to project development Feb 20, 2024 am 11:42 AM

1. Introduction to Python Python is a general-purpose programming language that is easy to learn and powerful. It was created by Guido van Rossum in 1991. Python's design philosophy emphasizes code readability and provides developers with rich libraries and tools to help them build various applications quickly and efficiently. 2. Python basic syntax The basic syntax of Python is similar to other programming languages, including variables, data types, operators, control flow statements, etc. Variables are used to store data. Data types define the data types that variables can store. Operators are used to perform various operations on data. Control flow statements are used to control the execution flow of the program. 3.Python data types in Python

See all articles