fervor [>]CODING & CURIOSITY
FERVOR LEARNING SYSTEMTUTORIALS
← JavaScript

JavaScript / 3 MIN READ

fromCharcode()

Convert character codes to strings

From the original Fervor library. Examples may use older package versions.

JavaScript’s .fromCharCode() Method: Converting Unicode to Strings

In this tutorial, we’ll explore the .fromCharCode() method in JavaScript. This built-in function converts sets of Unicode values (UTF-16 code units) into strings. You can utilize it for a variety of purposes, from generating random character strings to working with cryptography or encoding tasks. Let’s dive in and learn how to make the most of this method.

Introduction to .fromCharCode()

The .fromCharCode() method allows you to convert numeric Unicode values into corresponding characters within a string. It’s particularly handy when you need to transform Unicode values into actual characters, aiding tasks such as generating strings with specific characters, working with different character sets, and more.

Basic Usage

Here’s a basic example of using .fromCharCode() to create a string from an array of Unicode values:

const unicodeValues = [72, 101, 108, 108, 111];
const greeting = String.fromCharCode(...unicodeValues);

console.log(greeting);
// Output: "Hello"

In this example, we define an array unicodeValues that holds the Unicode values of characters forming the string “Hello.” The spread operator ... passes the array elements as individual arguments to .fromCharCode(), which converts them into a string stored in the greeting variable.

Use Cases

The .fromCharCode() method finds its applications in various scenarios:

  1. Generating Random Strings: By combining .fromCharCode() with Math.random(), you can create random strings. For instance, the code below generates a random string of 10 uppercase letters:
let randomString = '';
for (let i = 0; i < 10; i++) {
    const randomUnicode = Math.floor(Math.random() * 26) + 65;
    randomString += String.fromCharCode(randomUnicode);
}
console.log(randomString);
  1. Manipulating Character Sets: In cryptography or encoding tasks, .fromCharCode() can aid in converting characters between different sets or encodings. For instance, here’s how you might convert a UTF-8 string to ISO-8859-1 encoding:
const utf8String = 'Héllo';
let isoString = '';
for (let i = 0; i < utf8String.length; i++) {
    const unicode = utf8String.charCodeAt(i);
    if (unicode < 128) {
         isoString += String.fromCharCode(unicode);
    } else {
        isoString += String.fromCharCode(unicode + 848);
    }
}
console.log(isoString);
  1. String Manipulation: .fromCharCode() is versatile for creating and modifying strings. For instance, you can use it to replace all occurrences of a character in a string:
const inputString = 'Hello world';
const charToReplace = 'l';
const replacementChar = 'x';

let outputString = '';
for (let i = 0; i < inputString.length; i++) {
    const char = inputString.charAt(i);
    if (char === charToReplace) {
        outputString += replacementChar;
    } else {
        outputString += char;
    }
}
console.log(outputString);

Conclusion

The .fromCharCode() method in JavaScript is a powerful tool for converting Unicode values into strings, serving a range of purposes from generating random strings to working with character sets. By leveraging this method, you can enhance your code’s capabilities and efficiency.


Feel free to experiment with .fromCharCode() in various contexts, and explore its potential in character manipulation, encoding tasks, and more.

Home

Keep your curiosity going.Explore more JavaScript →
287 TUTORIALS · 22 TOPICSREADY