Data type of JS

Source: Internet
Author: User

JS has 5 basic data types and 3 reference types, five basic data types are number, String, Boolean, Undefined, Null, complex data types are mainly object, Function, Array.

typeof operator

Because the js variables in 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 null type has the typeof operator, the result is object, because the null type is treated as an empty object reference .

I. Basic data Types 1.Number Type

The number type contains both integers and floating-point numbers (floating-point numbers must contain a decimal point, with at least one digit after the decimal). Large or small numbers can be written by means of scientific (exponential) notation.

NaN: Non-numeric type. Features: ① any operation related to Nan, will return Nan②nan not equal to itself.

The IsNaN () function is used to check whether its arguments are non-numeric values.

IsNaN (123)//false IsNaN ("Hello")//true

2.String Type

String has the Length property.

String conversion: The transform function, string (), applies to any data type (null,undefined converted to null and undefined), and the ToString () method (null,defined does not have the ToString () method).

3.Boolean Type

The type has only two values, true and False

4.Undefined Type

There is only one value, that is, the undefined value. The variable is declared with Var, but the value of the variable is not initialized to the value of undefined.

5.Null Type

The null type is considered a null object pointer, and the null type is also an empty object reference.

6.Object Type

The object in JS is a set of properties and methods. This is about the reference type, which is a data structure used to organize data and functionality together. reference types are sometimes referred to as object definitions because they describe the properties and methods that a class of objects have.

two or three large reference types 1.Object Type

Most of the type values we see are instances of type object, and there are two ways to create an object instance.

The first is the use of the new operator followed by the object constructor, as shown below

var person = new Object ();

Person.name = "Micheal";

Person.age = 24;

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

var person = {

Name: "Micheal",

Age:24

};

2.Array Type

Each item of an array can be used to hold any type of data, that is, you can save the string with the first position of the array, the second position holds the value, and the third position holds 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 type is to use the array literal notation , as shown below

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

3.Function Type

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

function sum (num1,num2) {

return NUM1 + num2;

};

This is similar to the way you define functions using function Expressions .

var sun = function () {

return sum1 + sum2;

};

In other words, JS is divided into value types and reference types in storage mode. So what's the difference between their calculations?

Topic 1:var A = 100;

var B = A;

A = 200;

Console.log (b);

Topic 2:var A = {age:20};

var B = A;

B.age = 21;

Console.log (A.age);

The answer to Topic 1 is 100, the answer to question 2 is 21,

Topic 1 is a simple value type that, when you assign a base type from one variable to another, creates a new value on the variable and then copies the value to the location assigned to the new variable.

at this point, the value saved in a is 100, and when a is used to initialize B, the value saved in B is also 100, but 100 in B is completely separate from a, which is just a copy of the value in a, after which, These two variables can participate in any operation and are unaffected by each other. That is, after the assignment operation, the base type has two variables that are not affected by one another.

Topic 2 is a reference type, and when a value of a reference type is assigned to another variable from one variable, the value of the object stored in the variable is also copied into the space allocated for the new variable.

At this point in the variable is the address of the object in the heap memory, so, unlike the simple assignment, the copy of this value is actually a pointer, and this pointer points to an object stored in the heap memory. Then after the assignment operation,
Two variables all hold the same object address, the two variables point to the same object. Therefore, changing any one of these variables will affect each other.

Therefore, the assignment of a reference type is actually an assignment of the object's value in the stack address pointer, so that two variables point to the same object, and any operation will affect each other.

Data type of JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.