Home >Web Front-end >JS Tutorial >What are the js data types? Summary of data types in js

What are the js data types? Summary of data types in js

不言
不言Original
2018-08-07 15:27:113592browse

What are the data types in js? The data types of js are divided into basic types and reference types. Among them, there are five basic data types of js, namely: Number, String, Boolean, Undefined, and Null; there are three reference data types of js, namely: Object, Function, Array; this article will summarize the data types of js for everyone.

First, let’s take a look at the typeof operator:

Since variables in js are loosely typed, it provides a way to detect the data type of the current variable. , which is the typeof keyword.

typeof   123   //Number
typeof   'abc'  //String
typeof    true       //Boolean
typeof    undefined   //Undefined
typeof    null        //Object
typeof    { }           //Object
typeof    [ ]           //Object
typeof    console.log()       //Function

After the typeof operator is performed on the null type, the result is object. The reason is that the null type is treated as a null object reference.

1. js basic data types

1. Number type

The Number type contains integers and floating-point numbers (floating-point numbers must contain a decimal point and at least one digit after the decimal point). Very large or very small numbers can be written using scientific (exponential) notation.

NaN: non-numeric type. Features: ① Any operation involving NaN will return NaN ② NaN is not equal to itself.

isNaN() function is used to check whether its argument is a non-numeric value.

isNaN(123) //false isNaN("hello") //true

2. String type

The string has the length attribute.

String conversion: Transformation function String(), applicable to any data type (null, undefined, converted to null and undefined); toString() method (null, defined does not have toString() method).

3. Boolean type

This type has only two values, true and false

4. Undefined type

There is only one value, the undefined value. If a variable is declared using var, but the variable is not initialized, the value of the variable is undefined.

5. Null type

The Null type is regarded as a null object pointer. As mentioned above, the null type is also a null object reference.

6. Object type

Object in js is a collection of properties and methods. Here we talk about reference types. Reference types are a data structure used to organize data and functions together. Reference types are sometimes called object definitions because they describe the properties and methods of a class of objects.

2. js reference data type

1. Object type

Most of the type values ​​we see are Instances of the Object type. There are two ways to create Object instances.

The first way is to use the new operator followed by the Object constructor, as shown below

var person = new Object();
person.name = "Micheal";
person.age = 24;

The second way is to use the object literal notation, as shown below

var person = {
  name : "Micheal",
  age : 24
};

2. Array type

Each item of the array can be used to save any type of data, that is to say, the first position of the array can be used to save a string. , the second position stores the value, and the third position stores the object.... In addition, the size of the array can be dynamically adjusted.

There are two basic ways to create an array

The first is to use the Array constructor, as shown below

var colors = new Array("red","blue","yellow");

The second is to use the array literal representation, As shown below

var colors = ["red","blue","yellow"];

3. Function type

Each function is an instance of the Function type, and has the same properties and methods as other reference types. Functions are usually defined using function declaration syntax, as shown below

function sum(num1,num2){
  return num1 + num2;
};

This is similar to how functions are defined using function expressions.

var sun = function (){
  return sum1 + sum2;
};

In other words, js is divided into value types and reference types according to storage methods. So what is the difference between their calculations?

3. The difference between value type and reference type

1. Value type

occupancy The space is fixed and stored in the stack (when a method is executed, each method will establish its own memory stack, and the variables defined in this method will be put into this stack memory one by one. As the execution of the method ends, this The memory stack of the method will also be naturally destroyed. Therefore, all variables defined in the method are placed in the stack memory; the stack stores basic variables and reference variables of some objects, and the values ​​of the basic variables are stored in the stack. , and the reference variable stored in the stack points to the array or object address in the heap, which is why modifying the reference type always affects other reference variables pointing to this address.)

What is saved and copied is the value Itself

Use typeof to detect the type of data

Basic type data is a value type

2, reference type

占用空间不固定,保存在堆中(当我们在程序中创建一个对象时,这个对象将被保存到运行时数据区中,以便反复利用(因为对象的创建成本通常较大),这个运行时数据区就是堆内存。堆内存中的对象不会随方法的结束而销毁,即使方法结束后,这个对象还可能被另一个引用变量所引用(方法的参数传递时很常见),则这个对象依然不会被销毁,只有当一个对象没有任何引用变量引用它时,系统的垃圾回收机制才会在核实的时候回收它。)

保存与复制的是指向对象的一个指针

使用instanceof检测数据类型

使用new()方法构造出的对象是引用型

计算区别:

题目1:

var a = 100;
    var b = a;
      a = 200;
    console.log (b);

题目2:

var a = {age : 20};
    var b = a;
    b.age = 21;
    console.log (a.age);

题目1的答案是 100,题目2的答案是21,

题目1是简单的值类型,在从一个变量向另一个变量赋值基本类型时,会在该变量上创建一个新值,然后再把该值复制到为新变量分配的位置上。

此时,a中保存的值为 100 ,当使用 a 来初始化 b 时,b 中保存的值也为100,但b中的100与a中的是完全独立的,该值只是a中的值的一个副本,此后,这两个变量可以参加任何操作而相互不受影响。也就是说基本类型在赋值操作后,两个变量是相互不受影响的。

题目2是引用类型,当从一个变量向另一个变量赋值引用类型的值时,同样也会将存储在变量中的对象的值复制一份放到为新变量分配的空间中。

这时保存在变量中的是对象在堆内存中的地址,所以,与简单赋值不同,这个值的副本实际上是一个指针,而这个指针指向存储在堆内存的一个对象。那么赋值操作后,
两个变量都保存了同一个对象地址,则这两个变量指向了同一个对象。因此,改变其中任何一个变量,都会相互影响。

因此,引用类型的赋值其实是对象保存在栈区地址指针的赋值,因此两个变量指向同一个对象,任何的操作都会相互影响。

相关文章推荐:

js的数据类型及JS基本数据类型具体有哪几种

JS数据类型怎样转换

JS的数据类型+引用类型深浅拷贝与内存分配问题


The above is the detailed content of What are the js data types? Summary of data types in js. 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