Variables in JavaScript and data types in 9
5 ways to create variables in JS
Traditional solution
- var declares a variable
- function declares a function
Scheme in ES6
- let declare a variable
- const declares a variable that cannot be reassigned
Module scheme
import
9 data types in JS
Basic data type
- number
- string
- boolean
- null
- undefined
- symbol
- bigint
Reference data type
- object
{} ordinary object
[] array object
/^\d+$/ regular object
new Data() date object - function
Ordinary function
Arrow function
Constructor
Data type detection
- type of
- instanceof
- constructor
- Object.prototype.toString.call()
阿里巴巴最新面试题
Calculate the results of the following program operation and analyze the reasons
let arr=[10.18,0,10,25,23];
arr=arr.map(parseInt);
console.log(arr);
map(item,index) method: iterate five times, map will return item*10;
so after the first sentence is executed
arr=[101.8,0,100,250,230];
parseInt(value,radix)
1. Start the search from the first character on the left side of the value string and find all the values that match the radix hexadecimal (stop searching until a non-radix hexadecimal value is encountered)
2. The found value As a radix base, finally converted to decimal
3. radix disdain or write 0, the default value is 10 (the default radix is 16 for strings starting with 0x)
4. The value range of radix is between 2–36, not Within this range, the final processing result is all NaN
Convert an N-ary value to a decimal number
1. Each digit in the value *N^bit weight
2. Finally, the result of each digit calculation is accumulated
3. The single digit weight value is 0, ten digits 1, hundred's place 2
Equivalent statement | analysis | result |
---|---|---|
parseInt(‘10.18’,0) | 0 defaults to 10, find the decimal point ending '10' decimal number is itself | 10 |
parseInt(‘0’,1) | 1 is not in the range, it is directly NaN | NaN |
parseInt(‘10’,2) | '10' binary number converted to decimal to 2 | 2 |
parseInt(‘25’,3) | The ternary number 5 does not match, so only the '2' ternary to decimal is still 2. | 2 |
parseInt(‘23’,4) | '23' quaternary to decimal 2*4+3 | 11 |
Code execution and result comparison
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
let arr=[10.18,0,10,25,23];
arr=arr.map(parseInt);
console.log(arr);
</script>
</body>
</html>