curriculum/challenges/english/blocks/lecture-working-with-string-character-methods/672d266034b5242126271995.md
In programming, understanding how characters are represented as numbers is fundamental. This is where ASCII comes in. ASCII, short for American Standard Code for Information Interchange, is a character encoding standard used in computers to represent text. It assigns a numeric value to each character, which is universally recognized by machines.
In this lesson, we will explore what ASCII is, how it works, and how JavaScript methods like charCodeAt() and fromCharCode() relate to character encoding. While JavaScript strings use Unicode (UTF-16) internally, ASCII values match the first 128 Unicode characters, which is why ASCII-based examples work in JavaScript.
ASCII is a system for encoding characters such as letters, digits, and symbols into numerical values. Each character is mapped to a specific number.
For example, the capital letter A is represented by the number 65 in ASCII, while the lowercase a is represented by 97. This encoding allows computers to store and manipulate text.
The ASCII standard covers 128 characters including:
In JavaScript, you can access the numeric code of a character using the charCodeAt() method. This method returns the UTF-16 code unit of the character at a specified index. For the first 128 characters, this value matches the ASCII code.
Let’s take a look at an example:
:::interactive_editor
let letter = "A";
console.log(letter.charCodeAt(0)); // 65
:::
In this example, A is the first character of the string, and calling charCodeAt(0) returns its numeric code (which matches its ASCII value for basic Latin characters), 65.
You can also use this method with other characters to find their numeric code values:
:::interactive_editor
let symbol = "!";
console.log(symbol.charCodeAt(0)); // 33
:::
Here, the numeric code for the exclamation mark ! is returned as 33 (which matches its ASCII value).
While charCodeAt() helps you retrieve the numeric code of a character, the fromCharCode() method allows you to do the opposite: convert a UTF-16 code unit (which matches ASCII for basic characters) into its corresponding character.
Let's see this in action:
:::interactive_editor
let char = String.fromCharCode(65);
console.log(char); // A
:::
In this example, fromCharCode(65) converts the numeric code 65 (which matches the ASCII value for A) back to the character A.
Another example would be converting the number 97 to its corresponding lowercase letter:
:::interactive_editor
let char = String.fromCharCode(97);
console.log(char); // a
:::
These methods are particularly useful when you need to manipulate or compare characters based on their numeric code values.
For instance, you might use charCodeAt() to check if a character is uppercase, lowercase, or a digit by comparing its ASCII value.
On the other hand, fromCharCode() can be used to dynamically generate characters from their ASCII codes.
What does the charCodeAt() method return when used on a string in JavaScript?
The number of characters in the string.
Think about how characters are represented as numeric code values in JavaScript.
The index of a character in the string.
Think about how characters are represented as numeric code values in JavaScript.
The UTF-16 code unit of a character at a specified index.
The hexadecimal representation of a character.
Think about how characters are represented as numeric code values in JavaScript.
3
What will the following code output?
console.log(String.fromCharCode(66));
B
b
Refer to the section of the lesson that discusses fromCharCode().
6
Refer to the section of the lesson that discusses fromCharCode().
A
Refer to the section of the lesson that discusses fromCharCode().
1
Which of the following is an example of how character encoding is useful in programming?
To check if a string contains only uppercase letters.
Think about what you can do when characters are represented by their numeric code values.
To calculate the length of a string.
Think about what you can do when characters are represented by their numeric code values.
To convert a number into a floating-point value.
Think about what you can do when characters are represented by their numeric code values.
To manipulate characters based on their numerical values.
4