Answers for "compare function in js"

0

string comparison in javascript

var str1 = "ab";
var str2 = "ab";
var n = str1.localeCompare(str2);
Posted by: Guest on April-06-2021
13

javascript sort

var names = ["Peter", "Emma", "Jack", "Mia", "Eric"];
names.sort(); // ["Emma", "Eric", "Jack", "Mia", "Peter"]

var objs = [
  {name: "Peter", age: 35},
  {name: "Emma", age: 21},
  {name: "Jack", age: 53}
];

objs.sort(function(a, b) {
  return a.age - b.age;
}); // Sort by age (lowest first)
Posted by: Guest on May-21-2020
-1

string comparison in javascript

console.log("10" == "10"); //True
console.log(parseInt(10) == 10); // True Best practice
Posted by: Guest on December-06-2020

Browse Popular Code Answers by Language