You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In JavaScript, the built-in `sort()` method sorts the elements of an array. It returns a sorted array in ascending order. Note that each element will be converted to strings and then compared according to the sequences of UTF-16 code unit values. What does it mean?
3253
+
In JavaScript, the built-in `sort()` method sorts the elements of an array. It returns a sorted array in ascending order. Note that each element will be converted to strings and then compared according to the sequences of UTF-16 code unit values. What does it mean?
3255
3254
3256
3255
It means, "banana" < "cherry" or 80 < 9 (because "80" < "9" in the Unicode order).
3257
3256
3258
3257
If you run the following code `constresult= [9, 11, 89].sort();`, the constant `result` will be sorted as `[11, 8, 9]` rather than `[9, 11, 89]` because the engine will convert the number value to string.
3259
3258
3260
3259
The following codes might give you a hint about the relationship between character and number. Ultimately, as the computer can only understand 0 and 1, all characters and even decimal numbers are then converted to 1 and 0. `charCodeAt()` gives us the decimal value of any string evaluated.
3261
3260
3262
-
`console.log("LuKaKUUUU".charCodeAt(0))` or `console.log("LuKaKUUUU".charCodeAt())` ==> 76
3263
-
`console.log("Ronaldo".charCodeAt(0))` or `console.log("Ronaldo".charCodeAt())` ==> 82
3264
-
`console.log("messi".charCodeAt(0))` or `console.log("messi".charCodeAt())` ==> 109
3265
-
`console.log("neymar".charCodeAt(0))` or `console.log("neymar".charCodeAt())` ==> 110
3266
-
`console.log("ronaldo".charCodeAt(0))` or `console.log("ronaldo".charCodeAt())` ==> 114
3267
-
`console.log("9".charCodeAt())` or `console.log("99".charCodeAt())` ==> 57
3268
-
`console.log("80".charCodeAt())` or `console.log("8".charCodeAt())` ==> 56
3261
+
`console.log("LuKaKUUUU".charCodeAt(0))` or `console.log("LuKaKUUUU".charCodeAt())` ==> 76
3262
+
`console.log("Ronaldo".charCodeAt(0))` or `console.log("Ronaldo".charCodeAt())` ==> 82
3263
+
`console.log("messi".charCodeAt(0))` or `console.log("messi".charCodeAt())` ==> 109
3264
+
`console.log("neymar".charCodeAt(0))` or `console.log("neymar".charCodeAt())` ==> 110
3265
+
`console.log("ronaldo".charCodeAt(0))` or `console.log("ronaldo".charCodeAt())` ==> 114
3266
+
`console.log("9".charCodeAt())` or `console.log("99".charCodeAt())` ==> 57
3267
+
`console.log("80".charCodeAt())` or `console.log("8".charCodeAt())` ==> 56
3269
3268
3270
3269
Noted that if index is not a number, it defaults to 0. The answer is A.
3271
3270
@@ -3277,35 +3276,34 @@ Noted that if index is not a number, it defaults to 0. The answer is A.
3277
3276
###### 80. What's the output?
3278
3277
3279
3278
```javascript
3280
-
3281
-
constanArray=typeof [];
3282
-
constaTypeOfNull=typeofnull;
3279
+
constanArray=typeof [];
3280
+
constaTypeOfNull=typeofnull;
3283
3281
3284
3282
constweirdFirst=nullinstanceofObject;
3285
3283
constweirdSecond= [] instanceofObject;
3286
3284
constweirdThird= [] instanceofArray;
3287
3285
3288
-
console.log(anArray)
3289
-
console.log(aTypeOfNull)
3290
-
3291
-
console.log(weirdFirst)
3292
-
console.log(weirdSecond)
3293
-
console.log(weirdThird)
3286
+
console.log(anArray);
3287
+
console.log(aTypeOfNull);
3294
3288
3289
+
console.log(weirdFirst);
3290
+
console.log(weirdSecond);
3291
+
console.log(weirdThird);
3295
3292
```
3296
-
- A: "array" - "null" - false - true - true
3297
-
- B: "array" - "object" - false - true - true
3298
-
- C: "object" - "object" - false - false - true
3299
-
- D: "object" - "object" - false - true - true
3293
+
3294
+
- A: "array" - "null" - false - true - true
3295
+
- B: "array" - "object" - false - true - true
3296
+
- C: "object" - "object" - false - false - true
3297
+
- D: "object" - "object" - false - true - true
3300
3298
3301
3299
<details><summary><b>Answer</b></summary>
3302
3300
<p>
3303
3301
3304
-
In the 80th challenge question, we will review some fundamental "issue" or "weird" features in JavaScript relating to the `typeof` and `instance` operators. Given that the original version of the JavaScript language was designed in just 10 days, there are a bundle of inconsistent behaviors that cannot be fixed. They are pamernal features existing in the modern language. If we fix it, a lot of websites might crash.
3302
+
In the 80th challenge question, we will review some fundamental "issue" or "weird" features in JavaScript relating to the `typeof` and `instance` operators. Given that the original version of the JavaScript language was designed in just 10 days, there are a bundle of inconsistent behaviors that cannot be fixed. They are permanent features existing in the modern language. If we fix it, a lot of websites might crash.
3305
3303
3306
3304
The above code shows us some of the weird features in JavaScript. For example, `[]` is an array but the `typeof []` gives us `object`. Note that you might take advantage of `Array.isArray([])` rather than `typeof` to examine whether a variable is an array or not.
3307
3305
3308
-
`typeofnull;` is another weird operator as it returns `object`. However `nullinstanceofObject;` returns `false`. What~The~Hell!!!
3306
+
`typeofnull;` is another weird operator as it returns `object`. However `nullinstanceofObject;` returns `false`. ~WhatTheHell~!!!
3309
3307
3310
3308
Man, `[] instanceofObject;` and `[] instanceofArray;` both return `true`. How inconsistent it is.
0 commit comments