When it comes to the work with strings, JavaScript has solid functionality out-of-box. Take the String.prototype.charAt() method as an example that best illustrates how string objects work. This method returns a new string, which consists of a single UTF-16 code unit. In turn, this unit is located at a determined offset into the string.

Syntax

How do you express this JavaScript method in your code? Mind that the only correct use of charAt() in terms of syntax is:

"str".charAt(index);

As for the parameters, the index, in this instance, is an integer between 0 and str.length -1. In other words, it might happen that an index cannot be converted into an integer properly. Consequently, the default is 0, and thus the first character of str is returned to you.

As for the return value, please note that a string represents a character within the UTF-16 range. However, it does so only when the index is specified explicitly. It might also happen that the index is out of range. This object will return an empty string in JavaScript in such an instance.

When it comes to some use cases, charAt() method is mainly used to return characters at a specific range within a string. For a better understanding of this logic, consider the following example:

const text = "LEARNING JAVASCRIPT";
const letter = text.charAt(text.length-1);

In this code, charAt() is used to work with the length of a predefined string. If you run this code through a compiler, the result you'll get will be the last character of a string. In this instance, T would be your return.

Typical errors

However, what if we're trying to find something within a string that is absent in it originally? In this case, let's review the following code example:

const str = 'JavaScript is an easy-to-learn programming language';
print(str.charAt(50));

We give a value to a string using const and write a sentence that "JavaScript is an easy-to-learn programming language."

At the same time, you use the charAt() method to find a character in the index 50 in the initial string. The problem here is that no such character is present in the string, resulting in an empty return. Please note that this empty return is not void but rather an empty string.

Common use

Another great example that shows how JavaScript's charAt() method works is a variable called someString. Before explaining how the two work together, let's look at this code:

var someString = 'JavaScript';
 
console.log(someString.charAt(0));
console.log(someString.charAt(1));
console.log(someString.charAt(2));
console.log(someString.charAt(3));

We initially specified a variable someString. This variable is then assigned to the string value of 'JavaScript.' After that, we use the console.log to use the charAt() method to return a character at a specific position. If you run this code on your own, your return will look like this:

J
a
v
a

You might also wonder why the first letter is J when the char.At() method starts with 0. If you were attentive enough, you've probably found out that J is located at position 0 of the string. In other words, the count within the string starts from 0, which is true for all cases of using the char.At() method in JavaScript.

Call with no parameter set

Before moving forward, let us show you one more example with a someString variable. Consider using the following code:

const someString = 'JavaScript';
 
console.log(someString.charAt());

We also specify the string value and assign it to the someString variable. However, in this case, we leave the charAt()empty, without specifying parameters to work with the string. What happens when we do so, considering we've used the same example as before?

When no position is set, the someString variable's behavior is similar to the regular use of the charAt() method. In particular, your output will be J, and you won't be able to modify parameters at that stage.

If you follow the same logic, the someString variable will behave just in the same way if you lean on using a charAt() method with a position value out of bounds. For a better understanding of how it works, one should take a look at one more example:

const someString = 'JavaScript';
 
console.log(someString.charAt(52));

While the code is similar to the one used before, the return would be an empty string. Let us reiterate this rule once more: a position parameter has to be a value between 0 and str.length -1.

In this case, we also get an empty string as a return, mainly because the position parameter is set to 52, which is out of bounds for this particular case.

Summary

The charAt() method has a limited range of use cases when working with JavaScript. In most cases, you'll use this method to return a string consisting of the single UTF-16 code unit. For more advanced purposes, this statement can also be used to experiment with getting whole characters from a string and displaying characters at different locations in a string.

However, in most cases, you'd use the following syntax to work with characters within a string:

charAt(index)

Don't forget to use the someString variable with no conflicts when trying this method. Applying such an approach will allow assigning this variable to the string and further work with it using various commands.