SoFunction
Updated on 2025-04-06

Detailed explanation and application summary of fromCharCode and fromCodePoint in JavaScript

In JavaScript, string processing is a very important operation, and character encoding is the basis of string operation. This article will introduce in detail the JavaScriptandmethod. These two methods can help developers efficiently handle the conversion between characters and encodings, understand their differences and applications, and will make your code more flexible and efficient.

1. Method Overview

1. Method introduction

Is a static method built into JavaScript that converts a set of Unicode values ​​into corresponding characters and returns a string. This approach is very suitable for handling simple character encoding conversions.

grammar:

(num1, num2, ..., numN);

num1, num2, ..., numNis the Unicode encoded value to be converted to characters. One or more numbers can be passed in.

Example:

((65)); // Output "A"((97)); // Output "a"((8364)); // Output "€"

In this example, we pass in different Unicode encoded values, returning characters respectively'A''a'and'€'

2. Usage scenarios

fromCharCodeTypically used to convert integer Unicode encoded values ​​into characters, for example:

  • Character encoding conversion: You may need to get the corresponding characters from the character encoding.
  • Generate character sequences: Sometimes we need to generate characters based on a series of Unicode encoded values, such as generating alphabetical sequences from numeric codes.

2. Method Overview

1. Method introduction

is a more powerful method introduced by ES6, which is related toSimilar, but can handle a wider set of characters. andfromCharCodeOnly 16-bit encoded characters can be handled.fromCodePointSupports 21-bit Unicode encoded values, so it can handle more characters, especially those that contain supplemental flat characters (such as Emoji, partial Asian characters).

grammar:

(num1, num2, ..., numN);

num1, num2, ..., numNis a Unicode encoding point, which can be any legal Unicode encoding value (including 16-bit and 32-bit encoding).

Example:

((65));    // Output "A"((128512)); // Output "😀"((128525)); // Output "😍"

In this example,Ability to handle Emoji characters and traditional character encoding. It supports higher range of encodings and is able to handle more complex character sets.

2. Usage scenarios

fromCodePointIt is more suitable for scenarios where Unicode supplementary flat characters need to be handled, especially:

  • Emoji processing: In modern applications, the use of Emoji emoji has become very common, and the Unicode encoding values ​​of these characters are beyond the 16-bit range.fromCodePointIt can solve this problem perfectly.
  • Cross-language character processing: When dealing with some complex language characters, such as some unconventional East Asian characters, or relatively unpopular symbols,fromCodePointProvides higher character support.

3. The difference between fromCharCode and fromCodePoint

1. Supported character ranges

  • Only 16-bit encoding range is supported, i.e.0arrive0xFFFF(0 to 65535), so it can only handle characters in basic multitext planes.
  • Supports a wider range of characters including 32-bit encoding points ranging from0arrive0x10FFFF, which can handle Unicode supplemental flat characters.

2. Return value when used

  • Treat each parameter passed in as a 16-bit integer, returning the corresponding character. If the incoming integer exceeds the 16-bit range, it will be treated as an error.
  • This allows larger integers to be passed in and will be automatically converted to characters regardless of whether they are in the 16-bit range.

3. Application scenarios

  • If you only need to deal with common characters (such as letters and regular symbols),fromCharCodeTotally enough.
  • If you need to deal with Emoji, special symbols, or other supplemental flat characters, thenfromCodePointIt is essential.

IV. Application examples of fromCharCode and fromCodePoint

1. UsefromCharCodeGenerate character sequences

Sometimes you need to generate a string composed of continuous characters, which can be usedfromCharCodeTo achieve it.

let start = 65; // 'A'
let end = 90;   // 'Z'
let result = '';
for (let i = start; i <= end; i++) {
    result += (i);
}
(result);  // Output "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

In this example, we generate a'A'arrive'Z'alphabetical sequence. Through loop andfromCharCode, we are able to generate character sequences dynamically.

2. UsefromCodePointProcessing Emoji characters

Emoji is a very common element in modern applications. We can usefromCodePointto process and display these symbols.

let emojis = [128512, 128525, 128540];
let result = (code => (code)).join(' ');
(result); // Output "😀 😍 😐"

In this example, we generate corresponding characters by passing in Unicode encoded values ​​of Emoji and splicing them into a string output.

3. Application in string conversion

Sometimes we need to do some character encoding conversion to the string. For example, convert some letters in a string to their Unicode-encoded values:

let str = "Hello";
let result = (str).map(char => (0));
(result); // Output [72, 101, 108, 108, 111]

Here we passcharCodeAtGet the Unicode encoded value of each character and get the character'H'The code for72, and so on.

5. Things to note and summary

1. Choose the right method

  • For simple character set processing,It is a lighter and more commonly used method.
  • For applications that need to handle larger range characters such as Emoji and extended character sets,It is a better choice.

2. Performance considerations

AlthoughfromCharCodeandfromCodePointThe performance differences are not significant, but in some scenarios where a large number of character processing is required, it is recommended to choose an appropriate method. For example, when generating a large range of character sequences, usefromCodePointIt will be more convenient.

3. Extensive application of string processing

These two methods are very commonly used in JavaScript, whether it is character encoding conversion, display of Emoji emoticons, or generating dynamic character sequences.fromCharCodeandfromCodePointAll provide great convenience for developers.

This is the article about the detailed explanation and application summary of fromCharCode and fromCodePoint in JavaScript. For more related js fromCharCode and fromCodePoint content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!