console.log(3 > 2 > 1); // false
因為
先比較 3>2 // true
後比較 true > 1
根據之前提及的 coercion變成Boolean(true) > 1 // false
console.log(3 > 2 > 1); // false
因為
先比較 3>2 // true
後比較 true > 1
根據之前提及的 coercion變成Boolean(true) > 1 // false
console.log(typeof typeof 1); // string
This actually returns "string".
This expression is evaluated from right to left.
The first sub-expression evaluated actually is typeof 1 which will return "number".
Only after that the next sub-expression is evaluated which now is typeof "number" which returns "string".
console.log("This is a string." instanceof String); // false
This actually returns false.
The reason for that circumstance is that JavaScript distinguishes between primitives and objects.
And "This is a string." is actually a primitive string and not an instance of the object String.
If the code was like this:
new String("This is a string.") instanceof String
you'd actually get the result you would have expected at the beginning.
What instanceof actually does is checking if the String constructor is nested within the prototype chain of the value provided.
In this case, it isn't.
The plus operator is defined for numbers and strings and as soon as a string is present on either the left or right side, a string concatenation is perfomed.
// 1st step
'b' + 'a' -> 'ba'
// 2nd step
'ba' + + 'a' // wait a second!'a' to a number will actually yield NaN!// 1st step
'b' + 'a' -> 'ba'
// 2nd step
'ba' + + 'a' -> 'ba' + NaN -> 'baNaN'
// 3rd step
'baNaN' + 'a' -> 'baNaNa'
// 4th step
'baNaNa'.toLowerCase() -> 'banana'quiz:
// 1st step
false == '0'
// 2nd step
Number(false) == '0' -> 0 == '0'
// 3rd step
0 == '0' -> 0 == Number('0')
// 4th step
0 == 0 -> 0 === 0 -> true
solution:
there is a hierarchy for type coercion in JavaScript. The coercion process follows a set of rules:
null or undefined, they are considered equal (unless strict equality is being used with the "===" operator).valueOf() or toString() methods of the object.console.log(typeof NaN); // number
console.log(Number([])); // 0
console.log(Number(![])); // 0
console.log(Number([2])); // 2
console.log(Number([0,0])); // NaN
**
undefined means this thing has no value for some reason. Most of the times you encounter undefined it means that something has gone wrong.
On the other hand, null means this thing has no value, I recognise this and it's the way I would like it to be. 看見null表示是編寫者故意設的typeof(undefined) | Should output ← "undefined" |
typeof(null) | Should output ← "object"This is regarded by many as a mistake in the language. It's too late to fix it now so we'll have to deal with it. |
未看https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw