JS data type
The data type
Basic data types: Number, String, Boolean, NULL, undefind
Reference data types: function, Object, Arrary
Difference between NULL and undefind
null
Null indicates that the object is ready to be saved without actually saving its value. Logically, NULL represents an empty object pointer. Null is an object (empty object, with no properties or methods)
typeof null //Object
Typical uses of null:
- As arguments to a function, indicating that the function’s arguments are not objects
- As the end of the object prototype chain
undefined
Undefined: indicates a nonexistent value. Undefined is a primitive value that means “nothing” or something that means “missing value”, which means there should be a value, but it’s not defined yet, and undefined is returned when trying to get it.
typeof undefined //undefined
JS returns undefined:
- A variable is declared, but not assigned, equal to undefined;
let a
console.log(a) //undefined
Copy the code
- If the parameter to be provided is not provided when a function is called, the parameter is undefined.
function fn(a){
console.log(a) //undefined
}
fn()
Copy the code
- The object has no assigned attribute, the value of which is undefined;
let obj = {}
console.log(obj.name) //undefined
Copy the code
- If the function returns no value, undefined is returned by default
// Undefined is returned by default if the return value is not set
function fn(){}
let re = fn()
console.log(re) //undefined
// Set return, but do not explicitly return anything, return undefined
function fun(){
return
}
let re2 = fun()
console.log(re2) //undefined
Copy the code
- Returns undefined when a variable is assigned to undefined
let a = undefined
console.log(a) //undefined
Copy the code
Note: It is never recommended to display the value of setting a variable to underFed, but it should be set to NULL if the variable used to store the object does not have a real store variable. But undefined is actually derived from null and returns true in the equality test.
Simple data types
The number type
Number is a numeric type. In JS, we can also use the built-in number constructor to create a numeric value. Literal creation is recommended.
<script>
// Use the constructor to create a value
let x = new Number('10');
let y = new Number(5);
// the literal creates a numeric value
let z = 20;
// Check if it belongs to the same constructor
console.log(x.constructor === z.constructor);//true
</script>
Copy the code
Common methods for numeric types:
- ToFixed: Used to reserve several significant digits
- ParseInt: Used to convert to a number type and take an integer
- ParseFloat: Used to convert to a numeric type and take a floating point number
Type string
String is a string. In JS, we can also use the built-in string constructor to create a string value. Literal creation is recommended
<script>
//1. Create a literal
let str1 = 'Zhang Yi'
//2. Create a constructor
// Any string is equivalent to the instance object obtained by the new constructor
/* String objects have properties: 1. length: the property used to get the string 2. Index value: example: STR [0] can get the first character */
let str2 = new String('Zhang Yi')
</script>
Copy the code
Common methods for string types:
-
Split: Splits a string into an array, adding ” converts each character to the value of the array
-
ToLocaleLowerCase: Convert letters to lowercase
-
ToUpperCase: converts letters toUpperCase
-
IndexOf: The index value of the first occurrence of a character. If it is found, the index value of the first occurrence is returned. If it is not found, -1 is returned
-
LastIndexOf: The index value of the last occurrence of the character, returns the last occurrence of the index if found, or -1 if not found
-
Trim: Removes whitespace at both ends of a string
-
Substr: Truncate a string, indicating where to start truncating how many
Syntax: string. Substr (start,length)
Note: If there is only one parameter, it is truncated from the start position to the end
-
Substring: Intercepts a string. The beginning character can be retrieved but the end character cannot
Syntax: string. Substring (start index value, end index value)
Note: If there is only one parameter, it is truncated from the start position to the end
Boolean type
Boolean types have two values: true and false. In JS we can create Boolean values through Boolean. Literals are recommended.
Default false values in JS: undefind, null, “empty string, number 0, number NaN, false
<script>
// Use the constructor to create a Boolean type
let locked = new Boolean('10');
// literals create Boolean types
let flag = true;
// Check if it belongs to the same constructor
console.log(locked.constructor === flag.constructor);//true
</script>
Copy the code
Reference data type
The Object type
The object data type (object) can be created using the built-in object constructor to create a normal object. It is more recommended to create in literal form.
<script>
// Create a normal object through the constructor
let user = new Object({name: 'Ming'.age: 15});
// Object syntax abbreviation
let name = 'little red';
// create in literal form
let people = {
// Equivalent to name: name
name,
Function () {}
walk () {
console.log('Everybody walks... '); }}</script>
Copy the code
How common objects are stored in memory: The data of common objects is stored in heap memory, and the stack memory holds the address of common objects in heap memory. As shown in the figure:
Because the stack holds the address of heap memory, an assignment from an ordinary object assigns the address to another object. So whatever variable you change is going to change the data value of the other object pretty much. As shown in the figure:
Object method:
-
The object. assign static method creates a new Object
-
The object.keys static method gets all the properties of an Object
-
The object. values statement method gets the values of all attributes in an Object
Array type
The arrary type (Array) can be created using the built-in Array constructor to create an Array. It is more recommended to create in literal form.
<script>
// The constructor creates an array
let arr = new Array(5.7.8);
// Create an array literally
let list = ['html'.'css'.'javascript'];
</script>
Copy the code
Arrays are stored in memory just like normal objects, as shown below:
Array assignment copies only the address in the stack, not the data in the heap, so the value of the other object will be substantially changed regardless of which variable is changed. As shown below:
Array method:Array method:
-
Pop deletes the last item of the array and returns
-
Push appends an element to the end of the array and returns the length of the new array
-
Splice Deletes or replaces. Syntax: array. splice(delete element subscript, delete number, replace the deleted element with the value written by the third argument) puts the deleted element into the new array and returns
-
Shift deletes the first item of the array and returns the deleted element
-
Unshift appends to the front of the array and returns the length of the new array
-
The instance method concat merges two arrays to generate a new array
-
The instance method reverse reverses the array
-
The instance method join array element is concatenated into a string. Array.join() and so on. The concatenation between two pairs of values in parentheses defaults to,
-
IndexOf looks for the first occurrence of an index in an array and returns the first index if it finds it, or -1 if it doesn’t
-
The lastIndexOf method looks for the last occurrence of the index in the array
-
The static method from a pseudo-array is converted to an array. Note: To convert a pseudo-array to a real array, you must have the Length attribute
-
The static method isArray checks whether a is an array
-
Find is used to find the value that meets the condition for the first time and returns the value, or undefind if no value is found
-
FindIndex is used to find the value that meets the condition for the first time and returns the index value. If no value is found, -1 is returned
-
Some searches for a value that satisfies the condition and stops as soon as it finds one, returning true for some and false for none
-
Every is used to find elements that meet the criteria, and returns true if they do, or false otherwise
-
Filter is used to filter the elements that meet the criteria, and all elements that meet the criteria are placed in a new array and returned
-
Map is used to iterate over each element of an array, performing a callback function that returns the function being played into a new array
-
Example method forEach is used to iterate over groups of numbers instead of for loops (🌟)
< script > let arr = [" zhang fei ", "zhaoyun", "guan yu", "d", "huang zhong", "the sable cicada"]. ForEach ((item, index, x) => {// The first argument represents each item of the array, the current element console.log(item); // The second argument represents the index of each element of the array, the index of the current item console.log(index); // The third argument represents the current array's own console.log(x); }); </script>Copy the code
-
Sort
Arr.sort ((a,b)=>{return a-b})
Arr.sort ((a,b)=>{return b-a})