Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you think you have some skills to help others

help someone and become a part of our community - List Q & A

JavaScript - String charAt() method example

0 contributions
4 points
// 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

 

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more