JavaScript String at() Method



String at() Method

The JavaScript String at() method is used to retrieve a single character from a string at the specified position. It accepts an integer value and returns a new string containing a single UTF-16 code unit (the encoding system used for JavaScript strings).

It returns 'undefined' if the given integer value(index) is not found.

The at() and charAt() methods in JavaScript are quite similar to each other. However, there are key differences between them as follows −

  • The at() is a newer addition to JavaScript, while charAt() has been around for longer.
  • The at() method is more concise because it can read and use negative indexes, unlike charAt(), which does not support negative indexes and returns an empty string when given a negative index.

Syntax

Following is the syntax of JavaScript String at() method −

at(index)

Parameters

This method accepts a parameter named index, which is described below −

  • index − The index(or position) of the string character to be returned.

Return value

This method returns a new string containg a single character.

Example 1

Retrieving the first character of the given string.

In the given example, we are using the at() method to retrieve the first character of the given string "Tutorials Point". Since the index starts from 0 and ends str.length-1, we pass the index value as 0 to retrieve the first character.

<html>
<head>
<title>JavaScript String at() Method</title>
</head>
<body>
<script>
   const str = "Tutorials Point";
   document.write("String: ", str);
   const index = 0;
   document.write("<br>Index: ", index);
   document.write("<br>The first character of string '", str, "' is: ", str.at(index));
</script>
</body>
</html>

Output

The above program returns the first of string "Tutorials Point" as −

String: Tutorials Point
Index: 0
The first character of string 'Tutorials Point' is: T

Example 2

Retrieve the last character of the given string by passing the negative index to the at() method.

The following is another example of the at() method. We use this method to retrieve the last character of the given string "Hello World". Since this method can use and read the negative indexes, we pass the index value -1 to return the last character.

<html>
<head>
<title>JavaScript String at() Method</title>
</head>
<body>
<script>
   const str = "Hello World";
   document.write("String: ", str);
   const index = -1;
   document.write("<br>Index: ", index);
   document.write("<br>The last character of string '", str, "' is: ", str.at(index));
</script>
</body>
</html>

Output

After executing the above program, it returns a last character of string "Hello World" as −

String: Hello World
Index: -1
The last character of string 'Hello World' is: d

Example 3

Comparing the at() and charAt() methods

In the given example, we are comparing the at() and charAt() methods, and trying to retrieve the second-last element of the given string "JavaScript". The at() method handles the negative indexes, so index value -2 returns the second-last element, whereas the charAt() method takes index values as str.length-2 to return the second-last element.

<html>
<head>
<title>JavaScript String at() Method</title>
</head>
<body>
<script>
   const str = "JavaScript";
   document.write("String: ", str);
   document.write("<br>The second last element using at(-2) method: ", str.at(-2));
   document.write("<br>The second last element using charAt(str.length-2) method: ", str.charAt(str.length-2));
</script>
</body>
</html>

Output

The following output shows the differences between the at() and charAt() methods −

String: JavaScript
The second last element using at(-2) method: p
The second last element using charAt(str.length-2) method: p
Advertisements