EN
JavaScript - String charAt() method example
4 points
xxxxxxxxxx
1
var text = 'abc';
2
console.log( text.charAt( 0) ); // a
3
4
console.log( 'abc'.charAt(0) ); // a
5
console.log( 'abc'.charAt(1) ); // b
6
console.log( 'abc'.charAt(2) ); // c
7
8
console.log( text.charAt() ); // a
9
console.log( text.charAt(-1) == '' ); // true
10
console.log( text.charAt(10) == '' ); // true
11
console.log( text.charAt(null) ); // a
Syntax | String.prototype.charAt(index) |
Parameters | index - integer number value (primitive value). |
Result | Character from index position (primitive value). |
Description | charAt takes only one parameter and return single character from index position inside string. |
Bracket operator is alternative approach to get specific character from string.
xxxxxxxxxx
1
var text = 'abc';
2
3
console.log( text[0] ); // a
4
console.log( text[1] ); // b
5
console.log( text[2] ); // c
6
7
console.log( text[-1] ); // undefined
8
console.log( text[10] ); // undefined
9
console.log( text[null] ); // undefined