Javascript String charAt()

Introduction

Javascript String charAt() returns the character at a given index.

This method finds the 16 bit code unit at the specified index and returns the character that corresponds to that code unit:

let character = str.charAt(index)
  • index - an integer between 0 and 1-less-than the length of the string.

If no index is provided to charAt(), the default is 0.

let message = "abcde"; 
console.log(message.charAt(2));  // "c" 

Displaying characters at different locations in a string

let anyString = 'HTMLCSSJavaSQLJavascript';
// No index was provided, used 0 as default
console.log("The character at index 0   is '" + anyString.charAt()   + "'"); 
console.log("The character at index 0   is '" + anyString.charAt(0)   + "'");
console.log("The character at index 1   is '" + anyString.charAt(1)   + "'");
console.log("The character at index 2   is '" + anyString.charAt(2)   + "'");
console.log("The character at index 3   is '" + anyString.charAt(3)   + "'");
console.log("The character at index 4   is '" + anyString.charAt(4)   + "'");
console.log("The character at index 999 is '" + anyString.charAt(999) + "'");



PreviousNext

Related