Languages
[Edit]
EN

JavaScript - String charAt() method example

4 points
Created by:
JustMike
26120
// ONLINE-RUNNER:browser;

var text = 'abc';
console.log( text.charAt( 0) ); // a

console.log( 'abc'.charAt(0) ); // a
console.log( 'abc'.charAt(1) ); // b
console.log( 'abc'.charAt(2) ); // c

console.log( text.charAt()         ); // a
console.log( text.charAt(-1) == '' ); // true
console.log( text.charAt(10) == '' ); // true
console.log( text.charAt(null)     ); // a

1. Documentation

SyntaxString.prototype.charAt(index)
Parametersindex - integer number value (primitive value).
ResultCharacter from index position (primitive value).
DescriptioncharAt takes only one parameter and return single character from index position inside string.

2. String square bracket operator ([ ]) approach example

Bracket operator is alternative approach to get specific character from string.

// ONLINE-RUNNER:browser;

var text = 'abc';

console.log( text[0] ); // a
console.log( text[1] ); // b
console.log( text[2] ); // c

console.log( text[-1]   ); // undefined
console.log( text[10]   ); // undefined
console.log( text[null] ); // undefined

 

JavaScript - String

Native Advertising
50 000 ad impressions - 449$
🚀
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

❤️💻 🙂

Join