JS type (1) _JS Study notes (2016.10.02)

Source: Internet
Author: User

JS type

There are 6 types of data in JS, the first 5 is the original type (basic type), and the basic type of access is accessed by value, that is, you can manipulate the actual value saved in the variable undefined,boolean,number,string,null,object. The 6th is a reference type.

Basic type

The basic types have the following characteristics:

• The value of the base type is immutable. No method can change the value of a primitive type, such as a string:

var name = ' Jozo ';

Name.touppercase (); //Output ' Jozo '

Console.log (name); //Output ' Jozo '

The original name was not changed, but instead a new string was returned after the toUpperCase () method was called.

• Comparison of basic types is a comparison of values. They are equal only when their values are equal. But that might be the case:

var a = ' Jozo ';

var b = ' Jozo ';

Console.log (A = = B); //true

Some type conversions are performed when you compare two different types of variables with = =. A comparison like the one above will first convert true to the number 1 and then the number 1, and the result is true.

This is when the = = operator does the type conversion when comparing the two value types, but even = = = = = = = = = = = = = = = = = = = = = = = = = When the two values are of the same type.

• The basic type of variable is stored in the stack (stack memory in memory)

var name = ' Jozo ';

var city = ' Guangzhou ';

var age = 22;

The stack area includes the variable's identifier and the value of the variable.

Reference type

In addition to the basic type (number,string,boolean,null,undefined) above, JavaScript is a reference type,

It can also be said to be an object. Objects are collections of properties and methods. That is, a reference type can have properties and methods, and a property can contain both a base type and a reference type.

Reference types have the following characteristics:

• The value of the reference type is variable. We can add properties and methods to reference types, and we can delete their properties and methods, and they can be changed dynamically.

• The value of a reference type is an object that is stored in both the stack memory and the heap memory. An Action object is actually a reference to an operand, so the value of the reference type is accessed by reference.

To be precise, the storage of reference types requires a stack of memory and a heap area (the heap is referred to as heap memory in memory) to be done together.

The stack memory holds the variable identifier and the pointer to the object in heap memory, or the address of the object in the heap memory. If there are several objects:

var Person1 = {name: ' Jozo '};

var Person2 = {name: ' Xiaom '};

var Person3 = {name: ' Xiaoq '};

• Comparison of reference types is a reference comparison.

var person1 = {};

var person2 = {};

Console.log (Person1 = = Person2); //False

Reference types are accessed by reference, in other words, if the address in the heap memory of the two objects is the same, it is clear that Person1 and Person2 are different in the heap memory, so the two are completely different objects, so return false.

• The assignment of a reference type is actually an assignment of the object's value in the stack address pointer. Two variables all hold the same object address, the two variables point to the same object. Therefore, changing any of these variables will affect each other:

var a = {}; //A saves an instance of an empty object

var B = A; //A and B all point to this empty object

A.name = ' Jozo ';

Console.log (A.name); //' Jozo '

Console.log (B.name); //' Jozo '

B.age = 22;

Console.log (B.age); //

Console.log (A.age); //

Console.log (A = = B); //True

Type comparison

• Comparator:==,!=,===,!==,>=,<=,>,<

= = always try to compare their values, if they are basic types, compare their values, and if the types are different, always try to convert.

= = = Comparison of identity, no conversion is compared

• Compare the conversion order of objects:

If an object is compared to a base type, it is called first, and then the object's toString is compared to the base type by calling the ValueOf. In other words, it turns into number type and then to string type.

If it is compared to Boolean, first convert true to 1,false to 0 and then compare.

If both sides are numbers, or can be converted to numbers, the numbers are compared.

If both sides are string, or can be converted to string, the string is compared.

If one side can be converted to a string, and the other can be converted to number, then attempts to convert the string to number and then NaN if the string cannot be converted to number, and returns false.

If an object participates in a comparison, the object is always attempted to be compared to number or string.

JS type (1) _JS Study notes (2016.10.02)

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.