Chapter 5, basic reference types

This is the 28th day of my participation in the August Wenwen Challenge. Check out the details: August Wenwen Challenge

5.3.3 String

12. LocaleCompare () method

The final method is localeCompare(), which compares two strings and returns one of the following three values.

  • Returns a negative value if the string should precede the string arguments in alphabetical order. (Usually -1, depending on the implementation associated with the actual value.)
  • Returns 0 if the string is equal to the string argument.
  • If in alphabetical order the string should come after the string arguments, a positive value is returned. (Usually 1, depending on the implementation associated with the actual value.)

Here’s an example:

let stringValue = "yellow"; 
console.log(stringValue.localeCompare("brick")); // 1 
console.log(stringValue.localeCompare("yellow")); // 0 
console.log(stringValue.localeCompare("zoo")); // -1
Copy the code

Here, the string “yellow” is compared to three different values: “brick”, “yellow”, and “zoo”. Brick “alphabetically precedes “yellow”, so localeCompare() returns 1. Yellow “equals “yellow”, so “localeCompare()” returns 0. Finally, “zoo” comes after “yellow”, so localeCompare() returns -1. Again, since the exact value returned may vary from implementation to implementation, it is best to use localeCompare() as in the following example:

function determineOrder(value) { 
 let result = stringValue.localeCompare(value); 
 if (result < 0) { 
 console.log(`The string 'yellow' comes before the string '${value}'.`); 
 } else if (result > 0) { 
 console.log(`The string 'yellow' comes after the string '${value}'.`); 
 } else { 
 console.log(`The string 'yellow' is equal to the string '${value}'.`); 
 } 
} 
determineOrder("brick"); 
determineOrder("yellow"); 
determineOrder("zoo"); 
​
Copy the code

This ensures that string order is correctly determined in all implementations.

LocaleCompare () is unique in that the locale (country and language) in which the implementation is implemented determines how the method compares strings. In the US, English is the standard language for ECMAScript implementation, and localeCompare() is case-sensitive, with uppercase letters placed before lowercase letters. But that is not necessarily the case elsewhere.

13. The HTML method

Early browser developers saw a need to use JavaScript to dynamically generate HTML tags. As a result, early browsers extended the specification to include ways to assist in generating HTML tags. The following table summarizes these HTML methods. However, these methods have largely gone out of use because the results are usually not semantic tokens.

Original value packaging type is more finished today, more content, but also in the future we use JS language is very important cornerstone, must memorize, come on