This is the 22nd day of my participation in the August More Text Challenge
Persistence in learning, notes is the soul, review the old to learn new, from time to time, review the knowledge point, deepen memory, get twice the result with half the effort!
Earlier we looked at the basic data structures: objects and arrays
Today we’ll continue learning about JavaScript’s mysterious special “bugs “. Eg: [] ==! [] –> true???
As you can see on the cover, these expressions don’t look at the answer can you just say the correct answer?
1. Let’s look at the most basic Number
The picture above says:
Because of the Number.MAX_SAFE_INTEGER in JavaScript, most integers larger than this safe Number cannot be represented accurately.
2. Various equal signs in JavaScript:=
= =
= = =
So this equals equals, this is the assignment; == is loose equality operation, conversion type before comparison; === If the type is not the same, return false.
So using === is the equality operator we want to use and should avoid using == because you can see the following:
1= ='1' // true
1= = ='1' // false
Copy the code
Let’s take a look at this == in JavaScript, which we call abstract equality comparison. Comparing two values is looser with respect to ===, and implicit type conversions are also performed. For example:
[] == []
[] == ![]
Copy the code
Can you guess the result of the two lines above?
The first one returns false, which should be understandable: the two arrays are not the same object, so they are not equal. The second line is the magic of the mystery, it returns **true** can you believe it? So this is x equals not x…
An empty array is an object, it’s unreal so! [] is false. Using the == loose equality operator converts [] and false to numeric values for comparison, but neither is actually a numeric type.
Here we compare the type conversion logic of == : an empty array is cast to an empty string, which is then cast to 0. False is also converted to 0, which equals..
2. Object and array arithmetic operations+
[] + [] / /"
[] + {} // "[object Object]"
{} + {} // "[object Object][object Object]"
Copy the code
The two values are supposed to add up to a nonsensical value NaN, and the expressions here should all be NaN…
None of the arithmetic operations here are of numeric type, plus will concatenate them. The first is to convert operands to strings:
- Array with
Array.prototype.toString()
Method to an empty string, - But the goose object was
Object.prototype.toString()
Method is rendered as"[object Object]"
.
And so you get these weird strings
You think this is the end of it? Let’s take a look at this
{} + []
Copy the code
Guess what the result of this expression is? That’s right, you guessed wrong, it’s 0
In front of the above [], the two will be converted,
But if {}(empty object) comes first and [](empty array) follows, the preceding (left) operand ({}) is considered a block statement rather than an object literal.
So the expression {} +[] is the same as +[], which is the same as the Number([]) operation that forces the value of the Number, which is the same as the Number(“”) operation, which gives the Number 0.
There are more mysterious and weird phenomena, which are not listed here, but interested students can explore by themselves according to the cover image
Read more:
【 Data structure 】 data structure – objects and arrays (2) Array Array, 【 data structure 】 data structure – objects and arrays (1) Object, 【 data structure 】 in-depth understanding of JSON
Node.js file system server – emulated interface
[Node.js] file system module, [Node.js] HTTP module
[Node.js] efficiency tools – NVM & NRM, etc
Node.js Package management tool NPM and YARN
【Node.js】 Build automatic development environment – Basic introduction,
[Node.js] Installation & Documentation, [Tool Preparation], [Start], [Detailed Steps (4)], [Module processing tools (5)], [Understanding of modular programming]
【Github】 Multi-user collaboration (2), 【Github】 Basic use (1)
【Git】 Code version control – Basic Operation (1)
Keep up the pace, step by step! Updates below:
Next will continue to learn JavaScript related methods in detail, chong Duck!! xdm
Learn the efficiency tools to improve development efficiency and empower our development!
Keep up the pace, come on!! go~~