AppDividend
Latest Code Tutorials

Javascript String CharAt Example | String.prototype.charAt() Tutorial

0

Javascript String charAt Example | String.prototype.charAt() Tutorial is today’s topic. The String object’s charAt() method returns the new string consisting of a single UTF-16 code unit located at a specified offset into a string.  The charAt() method returns a character at the specified index in the string. The index of a first character is 0; the second character is 1, and so on. Javascript characters in a string are indexed from left to right. The index of a first character is 0, and the index of the last character in a string called is stringName.length-1. If an index you supply is out of range, then JavaScript returns the empty string.

If you want to learn more about the latest Javascript tutorials then check out this The Complete Javascript Course 2018.

Javascript String CharAt Example

If no index is provided to charAt() method, then the default is 0. The syntax for Javascript String charAt method is following.

string.charAt(index)

The index parameter is required. It is an integer representing the index of the character you want to return.

Let us take an example.

// app.js

let str = 'AppDividend';
let res = str.charAt(5);

console.log(res);

So, it will return the character at position 5, and the string’s index is starting from 0. So it should return 0.

 

Javascript String CharAt Example | String.prototype.charAt() Tutorial

Extract the first letter of every string in the array

We can extract the first letter of an array by the following code.

// app.js

const friends = ['Rachel', 'Chandler', 'Ross', 'Phoebe', 'Monica', 'Joey'];

extractedString = friends.map(i => i.charAt(0));

console.log(extractedString);

We have defined one array full of strings. Now, we need to only get only first character from each item. So first we map through each item and get the first character and add that characters into an array and return it. Since the map method is a pure function, it will return a new array.

 

String.prototype.charAt() Tutorial

Displaying the characters at different locations in the string

We can show all the characters like this.

// app.js

let OString = 'appdividend';

console.log("The character at index 0   is '" + OString.charAt(0)   + "'");
console.log("The character at index 1   is '" + OString.charAt(1)   + "'");
console.log("The character at index 2   is '" + OString.charAt(2)   + "'");
console.log("The character at index 3   is '" + OString.charAt(3)   + "'");
console.log("The character at index 4   is '" + OString.charAt(4)   + "'");
console.log("The character at index 5   is '" + OString.charAt(5)   + "'");
console.log("The character at index 6   is '" + OString.charAt(6)   + "'");
console.log("The character at index 7   is '" + OString.charAt(7)   + "'");
console.log("The character at index 8   is '" + OString.charAt(8)   + "'");
console.log("The character at index 9   is '" + OString.charAt(9)   + "'");
console.log("The character at index 10   is '" + OString.charAt(10)   + "'");

The output is like this.

 

Extract the first letter of every string in the array

Finally, Javascript String CharAt Example | String.prototype.charAt() Tutorial is over. Thanks.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.