The front end learns the data types based on day30&day31:02-JS

1. Data type

Data types are literal meaning and represent various data types. Data types exist in any language because data is diverse.

JavaScript mainly contains 8 data types, which can be divided into basic type and reference type:

  • Basic data type

    1. Number number (including integer and floating point numbers)

    2. String string

    3. boolean Boolean

    4. Undefined undefined

    5. Null null pointer

    6. Symbol symbol

    7. bigint large integer

  • Reference data type

    1. Object object

You can usually use the typeof operator to view the data type, but note that when detecting null values, the object type is returned instead of null. This is a special case.

2.Number

JavaScript does not distinguish between integers and floating-point numbers. They are all called number. typeof 100 gets "number".

Literal value: 10, 1.5, - 20

Floating point precision:

          console.log(0.1+0.2);

          console.log(0.7*100);

[IEEE 754 standard (opens new window)] is adopted in JavaScript( https://zh.wikipedia.org/wiki/IEEE_754 )64 bit double precision floating-point number. The operation of the value will first convert the value to binary, and the decimal may be incomplete under this standard, resulting in error in the final result.

If you want to get a relatively accurate result, you will generally convert the decimal to an integer before operation, and finally divide it by multiple. For example: console log( (0.1*100+0.2*100)/100 );

Value range:

According to the standard, the length of the exponential part of a 64 bit floating-point number is 11 binary bits, which means that the maximum value of the exponential part is 2047 (the 11th power of 2 minus 1). In other words, the maximum value of the exponential part of the 64 bit floating-point number is 2047, and half of it represents a negative number. Then the range of values that JavaScript can represent is 1023th power of 2 to 1024 power of 2 (open interval). Numbers beyond this range cannot be represented.

If a number is greater than or equal to the 1024 power of 2, a "forward overflow" will occur, that is, JavaScript cannot represent such a large number, and Infinity will be returned. In contrast, the maximum negative number is - Infinity.

Infinity and - infinity are also types of numbers.

Special value:

NaN is a special value. Its type is number. It represents a damaged value. It is usually generated when there is data that cannot be converted to numbers.

3.String

Used to put a paragraph of text. typeof "text" gets "string".

String literal:

"written words" // Double quotation mark
'ababa' // Single quotation mark
`abcd` // backquote 

All three quotation marks can be used to represent string data.

Escape character:

If you want to use quotation marks in a string:

console.log(  "It's an apple."  ); //There is no problem using the other two kinds of quotation marks in one kind of quotation marks

console.log( "John:\"I love you.\"" ); //If you use quotation marks with the same literal value internally, you need to use the \ escape symbol

Other escape meanings:

String splicing:

During + operation, if the data on either side is a string, it is the function of splicing

console.log("123" + "4"); //"1234"
console.log("123" + 4); //"1234"
console.log("ysc" + "666"); //"ysc666"

4.Boolean

Boolean value types have only two values: true and false. For judgment.

typeof true gets "boolean".

5.Undefined

The value of undefined type is undefined.

When the variable is not assigned, the default value is undefined.

typeof undefined gets "undefined".

6.Null

Null and undefined have similar meanings. They both mean "no". Null can be understood as an "empty" object, but it does not occupy memory space. Usually, when a variable is about to be given an object value in the subsequent logic, but the object value cannot be determined at the beginning of definition, give it the initial value null.

Note: typeof null gets "object"

7.Symbol

symbol is a data type with few application scenarios. This type of data will not be used in development, so you can understand it.

Symbol value cannot be calculated

Symbol is actually a primitive data type introduced by ES6, which is used to generate a unique value. In JS, the basic data type is usually true as long as it looks the same. In some specific cases, we may need some unique values to ensure the normal operation of the program. For example, when creating attributes for objects, the existing attributes will not be overwritten. Symbol is required at this time

Create a unique value to avoid a bug caused by name collision with the internal properties or functions of the object to be used

let s1 = Symbol() // Create a symbol data through the symbol function

let s2 = Symbol() // Create another one

console.log(s1) // Output result: Symbol()
console.log(s2) // Output result: Symbol()

// They look the same, but they are not equal
s1 == s2 // false

Conclusion: each call to Symbol() creates a unique value in the program

8.BigInt

This data type was added in the ES2020 version, so the browser environment before 2020 is not supported.

JavaScript has always been bad at numbers, because before bigint type, numbers can only represent values in the range of - (2 ^ 53-1) to 2 ^ 53-1, that is, number MIN_ SAFE_ Integer to number MAX_ SAFE_ Integer, the integer calculation or representation beyond this range will lose precision.

var num = Number.MAX_SAFE_INTEGER;  // -> 9007199254740991

num = num + 1; // -> 9007199254740992

// After adding + 1 again, the operation cannot be normal
num = num + 1; // -> 9007199254740992

// Two different values, but both return true
9007199254740992 === 9007199254740993  // -> true

As a result, BigInt came into being. It is the seventh primitive type, which can safely carry out large integer calculation.  

Function: ID and high-precision timestamp in the form of super large integer

In most cases, it can be used as a regular number type, such as +, -, /, *,%, and so on. For all operations of bigint, the returned result is also bigint

However, the result of 5n / 2n = 2n will cut off the decimal and retain the integer,% the same

Creating a value of BigInt type is also very simple, just add n after the number. For example, 123 becomes 123n. You can also use the global method BigInt(value) to convert the input parameter value into a number or a numeric string.

const aNumber = 111;
const aBigInt = BigInt(aNumber);
aBigInt === 111n // true
typeof aBigInt === 'bigint' // true
typeof 111 // "number"
typeof 111n // "bigint"

As long as n is added at the end of the number, the large number can be calculated correctly:

1234567890123456789n * 123n;
// -> 151851850485185185047n

However, there is a problem that BigInt and Number cannot be mixed in most operations. It is possible to compare Number and BigInt (= = not equal in strict comparison), but they cannot be added.

1n < 2 
// true

1n == 1 
//true

1n === 1 
//false

1n + 2
// Uncaught TypeError: Cannot mix BigInt and other types, use explicit conversions

unary operator +I won't support it
+'1' Will translate into number handle
+1n An error will be reported and the prompt cannot be converted to number

BigInt support:

9.Object type

The object type in JavaScript contains a lot of data. Arrays, ordinary objects, DOM nodes, built-in objects, functions, etc. all belong to obejct type.

Array:

An array can hold a set of data.

The value is indexed by [number s / N], and the S / N is counted from 0. When the value exceeds the maximum value of sequence number, undefined is obtained.

let arr = [10,50,true,"Fly"];

console.log(arr[2]); //true
console.log(arr[6]); //undefined

Arrays can be stored in arrays.

let arr = [
    10,
    [
        "Summer Gardenia",
        "lucky charm",
        [
            true,
            false
        ]
    ]
];

console.log(arr[0]); //10
console.log(arr[1][0]); //"Summer Gardenia"
console.log(arr[1][2][1]); //false

The array has the length attribute, which can get the number of data stored in the array.

let a = [10,20];
let b = [7,8,9];
let c = [4,5,,6,];

console.log(a.length); //2
console.log(b.length); //3
console.log(c.length); //4. If there is no value behind the last one, it is not counted. If there is no data between the middle ones, it is counted even if there is no data

The array can take values, modify values, or add new values

let arr = [4,5];
arr[0] = 44;
arr[2] = 6;
console.log(arr); // [44,5,6]

let arr2 = [7,8,9];
arr2.length = 2;
console.log(arr2); //[7,8]

Common objects:

Object stores data as key value pairs. The key is the attribute of the object, and the value is a specific data.

The naming rules of attributes and variables are somewhat similar, but attribute names are more relaxed. Property names are allowed to be numbers. Nonstandard property names can be added with "" to become a correct property name.

let xz = {
    name : "Summer Gardenia",
    "age" : 18,    //Property can be written in a string like manner or not
    "a b c" : true, //For irregular attribute names, you must add '', otherwise an error will be reported
    20 : null //Natural numbers can be used as property names without adding ""
};

Used when taking values Operator.

let xz = {
    name : "Summer Gardenia",
    age : 18,
    marry : false,
    friends : ["lucky charm","Si Si"]
};

console.log( xz.age ); //18
console.log( xz.friends[0] ); //"Koi"
console.log( xz.hobby ); //undefined

When the attribute is a data, use []

let xz = {
    name : "Summer Gardenia",
    age : 18,
    marry : false,
    friends : ["lucky charm","Si Si"]
};

console.log( xz.name ); //"Summer Gardenia"
console.log( xz["name"] ); //"Summer Gardenia"

let a = "age";
console.log( xz[a] ); //18
console.log( xz.a ); //undefined

Object can take values, re assign values, or add new attributes

let obj = {a : 10};

obj.a = 20;
obj.b = 30;

console.log(obj); // {a:20,b:30} 

Built in objects:

Objects that already exist in JavaScript syntax are called built-in objects. These objects generally contain many properties and methods, with sound and rich functions, which we can use directly. For example, window, document, Math.

Function:

Function in JavaScript is also an object type, which is a very special object.

Define function

let a = function(){
    //Any js code can be written here
};

function b(){
    //Any js code can be written here
}

Function execution

function fn(){
    alert(123);
}
//When the function is not executed, the internal function does not run.

//The function plus () can be self executing
fn();

For object type data, typeof will get object, but the function will get function when typeof.

10. Other precautions

Precision loss:

js indicates that there is a bug in floating-point numbers, for example, 0.1 + 0.2 = 0.3000000004

Indicates the range of values:

1023 ~ 1024 power of 2 [1023 power of 2, 1024 power of 2)

        

let num1 = 0b11    // Binary representation 3
let num2 = 0o70    //Octal representation 56
let num3 = 0xA //Hexadecimal representation 10
let num4 = +0
let num5 = 0 / 0    //NaN represents a broken value
let num6 = Number(122)

Quotation mark usage:

The quotation marks in the ordinary string should be inconsistent with the outer quotation marks. Single and double quotation marks can be used arbitrarily in the template string

String splicing:

js can identify both sides of the + sign. If the + side is a string, it is even string splicing

Representation of variables within a string:
Ordinary string: consistent with the quotation marks outside "+ variable +"
Template string: ${variable}

        string.length returns the length of a string

let str6 = String("ysc")

The function returns an undefined value by default when no return value is given

The operation undefined makes no sense unless it is used for judgment

Null represents an empty object. When declaring an object type, it can be assigned to null first (keep the type consistent)
Or manually assign null after the object is used

js can represent the largest integer: the 53rd power of 2 - 1

        Number.MAX_SAFE_INTEGER:js is the largest integer that can be represented
        Number. MIN_ SAFE_ Integer: the smallest negative integer that JS can represent

Definition method of BigInt type data:

Definition method 
let bigI = 9007199254740991n 
let bigI = BigInt(100)

Keywords: Javascript Front-end

Added by calande on Fri, 21 Jan 2022 07:54:17 +0200