Home >Web Front-end >JS Tutorial >Learn more about data types in JavaScript

Learn more about data types in JavaScript

青灯夜游
青灯夜游Original
2021-02-17 09:01:412069browse

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";
, 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个空间。

b. Access array elements:

数组名[下标]

c , Array element traversal iteration:

    Ordinary loop
  • for(let i = 0;i < array.length; i++) {
    	//array[i]}
    for-in
  • for(let 变量名 in 数组名) {
    	//变量名中所保存的值是数组下标编号的字符串内容
    	//仍然使用“数组名[字符串下标]”来访问数组对应下标处的元素}
    for-of( ES6)
  • for(let 变量名 of 数组名) {
    	//变量名中所保存的值是数组中当前便利到的元素值}
    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)
	}}

2. Constructor creation


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

3. Create


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

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 函数名(参数列表) {
	//函数主体:可被重复使用的代码块}

2), function expression


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

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


Call:

a. Direct call

函数名()

b. Event-driven


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

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 表达式;

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:
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