$. Data types and variables
1. No distinction between integers and floating-point numbers, unified with number
2. Strings string
3. Boolean values
Attention:
(1)
In fact, JavaScript allows comparisons of arbitrary data types;
Pay special attention to the equality operator ==
.
The second is the ===
comparison, which does not automatically convert the data type if the data type is inconsistent, returns false
if consistent, and then compares.
Because of this design flaw in JavaScript, do not use ==
comparisons, always insist on using ===
comparisons.
(2)
Another exception is that NaN
this particular number is not equal to all other values, including itself:
NaN === NaN; // false
The only way to judge NaN
is through the isNaN()
function:
isNaN(NaN); // true
(3)
Finally, pay attention to the equal comparison of floating-point numbers:
1 / 3 === (1 - 2 / 3); // false
This is not a design flaw in JavaScript. Floating-point numbers generate errors during operation because the computer cannot accurately represent infinite loop decimals. To compare two floating-point numbers for equality, you can only calculate the absolute value of their difference to see if they are less than a certain threshold:
Math.abs(1 / 3 - (1 - 2 / 3)) < 0.0000001; // true
4.null and undefined
null
Represents an "empty" value, 0
unlike an empty string, which ‘‘
0
is a numeric value that ‘‘
represents a string of length 0 and null
represents "null".
The designer of JavaScript wants to null
represent an empty value, whereas the undefined
value is undefined. It turns out that there is no such thing as an egg, and the difference between the two is insignificant. In most cases, we should all use null
. undefined
useful only when judging whether a function parameter is passed or not.
Liaoche JavaScript Tutorial Learning Record