• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Actual Wizard

Actual Wizard

Great Content From Actual Wizards

  • Blogging
  • Programming
  • Social Media
  • Marketing
You are here: Home / Programming / JavaScript

JavaScript String fromCharCode Method Explained

August 21, 2020 by Kevin Marszalek

The fromCharCode() method allows JavaScript to create a string from a sequence of UTF-16 character codes and is useful for dealing with characters that are difficult to type or need to be escaped.

String.fromCharCode()

Note: This method is a static method of the object named string and is not an inherited method of a variable that is a string. If you attempt to call the method as if it were a string variable, you will generate the following error:

fromCharCode is not a function

The fromCharCode() method must be called as followed:

var MyString = String.fromCharCode(84,101,120,116);

Full Example of the fromCharCode() Method:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
	<meta content="utf-8" http-equiv="encoding">
	<title>JavaScript String fromCharCode Method Example</title>
</head>
<body>
<script>
	var MyText = "abc";
	MyText += String.fromCharCode(49,50,51);
	console.log(MyText);
</script>
</body>
</html>

The following text is printed to the console:

abc123

Valid Character Input Types for fromCharCode()

This method will accept character formatted three different ways:

Note: Each example will produce the word “HELLO.”

Decimals:

MyString = String.fromCharCode(72,69,76,76,79,46);

Hex Values:

MyString = String.fromCharCode(0x48,0x45,0x4c,0x4c,0x4f,0x2e);

Octal Values:

MyString = String.fromCharCode(0o110,0o105,0o114,0o114,0o117,0o56);

Limitations of the fromCharCode() Method

This method will not work correctly with escaped Unicode characters, but this isn’t a problem as a string can be assigned an escaped Unicode sequence instead.

var MyString = '\u0048\u0045\u004C\u004C\u004F\u002E';
//HELLO.

It is recommended that if you are working with complex characters that are outside of the standard set of characters known as the BMP (Base Multilingual Plane) that you use the fromCodePoint() method instead.

This is due to the limitation of the fromCharCode() method, which is limited to 16-bit values.

If for one reason or another, you must use fromCharCode() with characters outside of the BMP, then you can substitute a surrogate pair or character to represent it.

Filed Under: JavaScript

Read More From Actual Wizard

  • Understanding the Different Parts of an Email Address
    An email address has four parts; the recipient name, the @ symbol, the domain name, and the top-level domain. …
  • How to Get the Last Element of an Array in JavaScript
    There are a number of different ways to get the last element of an array in JavaScript. Below we will explore …
  • How to use document.write() in JavaScript
    How to use document.write() in JavaScript The document.write() function is commonly used when testing simple …
  • How to Open a New Tab with JavaScript
    Opening a new browser window in a new tab can be done easily in JavaScript. Modern web browsers provide a …

Primary Sidebar

More posts from this section

  • How to Get the Last Element of an Array in JavaScript
  • How to use document.write() in JavaScript
  • How to Open a New Tab with JavaScript
  • How to Convert an HTML Node to a String in JavaScript
  • JavaScript String Anchor Method Explained
  • JavaScript String Big Method Explained
  • JavaScript String Blink Method Explained
  • JavaScript String Bold Method Explained
  • JavaScript String charAt Method Explained
  • JavaScript String charCodeAt Method Explained

Copyright © 2023 ActualWizard.com

  • About
  • Terms of Service
  • Privacy Policy
  • Cookie Policy