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 this article, we will cover a range of JavaScript interview questions, including those related to the latest versions of the language (ES6, ES7, ES8, and ES9).
10
10
@@ -1024,14 +1024,14 @@ This is different from the previous example where ... spread operator was used,
1024
1024
<h3>34. What is the output of the following code?</h3>
1025
1025
1026
1026
```jsx
1027
-
var companies = [
1027
+
constcompanies= [
1028
1028
{id:"1", name:"Facebook"},
1029
1029
{id:"2", name:"Apple"},
1030
1030
{id:"3", name:"Google"},
1031
-
]
1031
+
];
1032
1032
1033
-
companies.sort((a,b) => (a.name>b.name?-1:1))
1034
-
console.log(companies)
1033
+
companies.sort((a,b) => (a.name>b.name?-1:1));
1034
+
console.log(companies);
1035
1035
1036
1036
```
1037
1037
@@ -1042,9 +1042,15 @@ The output of the code will be:
The comparison function takes two parameters, "a" and "b", which represent two elements being compared in the array. If the "name" property of "a" comes before the "name" property of "b" in alphabetical order, then the function returns -1, which means "a" should come before "b" in the sorted array. Otherwise, if the "name" property of "a" comes after the "name" property of "b" in alphabetical order, then the function returns 1, which means "b" should come before "a" in the sorted array.
1052
+
The comparison function takes two parameters, "a" and "b", which represent two elements being compared in the array.
1053
+
If the "name" property of "a" comes before the "name" property of "b" in alphabetical order, then the function returns -1, which means "a" should come before "b" in the sorted array.
1054
+
Otherwise, if the "name" property of "a" comes after the "name" property of "b" in alphabetical order, then the function returns 1, which means "b" should come before "a" in the sorted array.
0 commit comments