Basic data types

Js has five basic data types:

  • undefined
  • null
  • number
  • boolean
  • string
  • ES6 added a new base data type Symbol (used to indicate uniqueness)

The data is stored directly in the stack space, and the basic data types are accessed by value, meaning that we can manipulate the actual values stored in variables.

Values of basic data types are immutable

No method can change the value of a primitive type, such as a string:

var name = "change";
name.substr();//hang
console.log(name);//change

var s = "hello";
s.toUpperCase()//HELLO;
console.log(s)//hello
Copy the code

In both cases, we can see that the value of the previously defined variable name has not changed, and that the substr() and toUpperCase() methods return a new string unrelated to the previously defined variable name

What if we assign the change directly? Look at the code:

var name = "change";
name = "change1";
console.log(name)//change1
Copy the code

So it looks like the value of name is “changed”. In fact, var name = “change”, where the base type is string, which is “change”, where “change” cannot be changed

Name is just a pointer to “change”, the pointer can change, so you can name = “change1”. In this case, name refers to “change1”, and similarly, “change1” cannot be changed.

The base type is “change”, not name

You cannot add attributes and methods to base data types

var p = "change";
p.age = 29;
p.method = function(){console.log(name)};
console.log(p.age)//undefined
console.log(p.method)//undefined
Copy the code

From the above code, we know that you cannot add attributes and methods to primitive types.

A comparison of basic data types is a comparison of values

var person1 = '{}';
var person2 = '{}';
console.log(person1 == person2); // true
Copy the code

Ii. Cited data types

Object,Function, etc. (note that array. Date is classified as Object). They store a pointer in the stack space to the value in the heap space.

The value of a reference type can be changed

var o = {x:1};
o.x = 2;// Change an object by modifying its property value
o.y = 3;// Change the object again and add a property to it

var a = [1.2.3];
a[0] = 0;// Change an element of the array
a[3] = 4;// Add an element to the array
Copy the code

Reference types can add properties and methods

var person = {};
person.name = "change";
person.say = function(){alert("hello"); }console.log(person.name)//change
console.log(person.say)//function(){alert("hello"); }
Copy the code

Assignments of reference types are object references

Let’s start with the following code:


var a = {};
var b= a;
a.name = "change";
console.log(a.name)//change;
console.log(b.name)//change
b.age = 29;
console.log(a.age)/ / 29
console.log(b.age)/ / 29
Copy the code

When a value of a reference type is assigned from one variable to another, a copy of the value of the object stored in the variable is also made into the space allocated for the new variable. A reference type stored in a variable is the address of the object in heap memory.

So, instead of a simple assignment of a basic data type, the copy of this value is actually a pointer to an object stored in heap memory. So after the assignment, both variables hold the same object address, and these two addresses refer to the same object. So if you change any one of these variables, they affect each other.

A comparison of reference types is a comparison of references

var person1 = {};
var person2 = {};
console.log(person1 == person2)//false
Copy the code

Why do two objects look exactly the same but are not equal?

Because the comparison of reference type is = = = = reference comparison, in other words, is to compare two objects stored in the stack area refers to the heap memory address is the same, at this point, although the p1 and p2 seem to be a “{}”, but they are kept in the heap memory address in the stack area is different, so the two objects are not equal.

Iii. Basic Packaging type (Packaging object) :

Take a look at the following code:

var s1 = "helloworld";
var s2 = s1.substr(4);
Copy the code

We said that strings are primitive data types and should not have methods. Why can s1 call substr()?

ECMAScript also provides three special reference types: Boolean,String, and Number. We call these three special reference types basic wrapper types, also known as wrapper objects.

This means that when we read the basic data types string, Boolean, and number, the background == creates a corresponding object of the basic wrapper type, allowing us to call some methods to manipulate the data.

So when the second line of code accesses S1, the background automatically does the following:

var s1 = new String("helloworld");  // Create an instance of String;
var s2 = s1.substr(4);          // Call the specified method on the instance;
s1 = null;                      // Destroy the instance;
Copy the code

Because of the third destruction step, you can see why primitive data types can’t add attributes and methods, which is the main difference between primitive package types and reference types: object lifetime

  • Instances of reference types created using the new operator are kept in memory until the execution flow leaves the current scope
  • Objects of the basic wrapper type, created automatically, exist only for the moment a line of code is executed and then destroyed immediately.

4. Comb common problems

Undefind, null, NaN, void 0

  • Underfind: Missing value. There should be a value here that becomes NaN when converted to a number
  • Null: defined but null, converted to 0 after digitalization
  • Void 0: equals undefined
  • NaN: A special value that represents a non-numeric value. This property is used to indicate that a value is not a number.
		console.log("undefined == null".undefined= =null);     //true
		console.log("{} = = {}", {} = = {});//false
		console.log("NaN == NaN".NaN= =NaN);                   //false
		console.log("0 == undefined".0= =undefined);           //false
		console.log("void 0 == null".void 0= =null);           //true
Copy the code

Why void 0 instead of undefined?

Note: void is the unary operator that appears to the left of the operand, which can be any type of value. The expression to the right of void can be parenthesized (for example, void(0)) or unparenthesized (for example, void 0).

  • In ES5 global, undefined is read-only. In local scopes, undefined is mutable, so void 0 is recommended for undefind.

  • Using void 0 is 3 bytes less than using undefined

>"undefined".length
/ / 9
>"void 0".length
/ / 6
Copy the code
  • Undefined is not a reserved word in javascript; we can use undefined as a variable name and assign it a value. Void 0 outputs only undefined, which guarantees immutability.

Why is 0.1+0.2 == 0.3 false?

The JS number type only has the number type, which is equivalent to the double in other strongly typed languages and does not distinguish between floating point and integer.

Because all the number types of Js are double precision floating point type (64-bit) using IEEE754 standard 64-bit binary number to represent a number number

Where 64 bits = 1 sign bit + 11 exponent bit + 52 decimal bits

The loss of precision of floating-point numbers is caused by the fact that floating-point numbers cannot be directly converted to the standard binary, but can only be converted to an approximate value, so there is a loss of precision

Recommend solutions

  • To convert decimal operations to integer operations, use math.floor () : round down, math.ceil () : round up
  • Console. log(math.abs (0.1 + 0.2-0.3) <= number.epsilon);

How do I detect +0 and -0?

In addition to NaN, JS has two special numbers

  • Infinity c.
  • -infinity, minus Infinity.

JavaScript has +0 and -0, and there is no difference between them in addition classes, but you need to be careful about division. The “forget to detect divide by -0 and get negative infinity” situation often leads to errors,

And the way to tell the difference between +0 and -0 is to check whether 1/x is Infinity or -infinity.

What is the maximum length of a string?

String is used to represent text data. String has a maximum length of 2^ 53-1, which is sufficient for normal development,

But interestingly, the maximum length, as it’s called, isn’t exactly the number of characters you would expect. Because the meaning of String is not “String”, but the UTF16 encoding of String, our String operations charAt, charCodeAt, length and other methods are targeted at UTF16 encoding. So, the maximum length of a string is actually influenced by the encoding length of the string.

Note: The current international standard for character sets, in which characters are represented in Unicode. Each Unicode code point represents a character. In theory, the range of Unicode is unlimited. UTF is a Unicode encoding that specifies how code points are represented in computers. Common ones are UTF16 and UTF8. Unicode code points are usually U+??? In which?? Is a hexadecimal code point value. Code points from 0 to 65536 (U+ 0000-U +FFFF) are called basic character regions (BMP).Copy the code

That is, length does not return the intuitive length of the string directly, but rather the length of the encoded code point in Unicode encoding state.

For example, the length of a Korean character is 2.


Write in the last

Thank you for seeing this, you might as well like it before you go ~~