Javascript - String charAt() Method

The charAt() method returns the character at the specified index from a string.

Description

The charAt() method returns the character at the specified index from a string.

The index of the first character is 0, the second character is 1, and so on.

The index of the last character in a string is string.length-1, the second last character is string.length-2, and so on.

Syntax

string.charAt(index)

Parameter Values

Parameter Require Description
index Required. An integer representing the index of the character you want to return

Return

A String, representing the character at the specified index, or an empty string if the index number is not found

Demo

//Return the first character of a string:
var str = "HELLO WORLD";
var res = str.charAt(0)
console.log(res);/* w w w.ja v  a 2  s  .co m*/
//Return the last character of a string:
str = "HELLO WORLD";
res = str.charAt(str.length-1);
console.log(res);

Result