Character Methods

Description

Two String methods access specific characters: charAt() and charCodeAt().

They both accept zero-based position as the parameter.

charAt()

The charAt() method returns the character in the given position as a single-character string.


var stringValue = "hello world";
console.log(stringValue.charAt(1));   //"e"

The code above generates the following result.

charCodeAt()

charCodeAt() returns the character code instead of the actual character.


var stringValue = "hello world";
console.log(stringValue.charCodeAt(1));   //outputs "101"

This example outputs "101", which is the character code for the lowercase "e" character.

The code above generates the following result.

Bracket notation

You can also use bracket notation with a numeric index to access a specific character in the string.


var stringValue = "hello world";
console.log(stringValue[1]);   //"e"

The code above generates the following result.





















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions