Using String charAt() method to coding in Javascript/JS

This JavaScript tutorial explains how to use the string technique referred to as charAt() with syntax and examples.

Description

In JavaScript, charAt() is a string approach that is used to retrieve a persona at a unique position in a string. Because the charAt() approach is a method of the String object, it should be invoked through a precise occasion of the String class.

Syntax

In JavaScript, the syntax for the charAt() technique is:

string.charAt([position]);

Parameters or Arguments

position Optional. It is the function of the persona in string that you wish to retrieve. The first role in the string is 0 If this parameter is not provided, the charAt() method will use zero as the default.

Returns

The charAt() method returns a string representing a personality at a precise position in a string.

The role ought to be between zero and string.length-1. If the position is out of bounds, the charAt() method will return an empty string.

Note

The charAt() technique does not change the value of the original string.

Example

Let’s take a appear at an instance of how to use the charAt() method in JavaScript.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.charAt(0));
console.log(totn_string.charAt(1));
console.log(totn_string.charAt(2));
console.log(totn_string.charAt(3));

In this example, we have declared a variable called totn_string that is assigned the string fee of ‘TechOnTheNet’. We have then invoked the charAt() technique of the totn_string variable to return a character at a particular position.

We have written the output of the charAt() technique to the internet browser console log, for demonstration purposes, to exhibit what the charAt() approach returns.

The following will be output to the net browser console log:

T
e
c
h

As you can see, the charAt() approach lower back a single persona from the string in all 4 cases. The first name to the charAt() technique lower back “T” which is the personality at function 0 of the string. The 2d call back “e” which is the personality at function 1. The 0.33 name lower back “c” which is the personality at function two The fourth call lower back “h” which is the personality at position 3

No Parameter is Provided

Next, let’s see what occurs if you don’t supply a position parameter to the charAt() method.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.charAt());

The following will be output to the net browser console log:

T

When no position parameter is provided, the charAt() approach will use 0 as the price of the role parameter. In this example, the charAt() method back the first character in the string (which is role 0) when no parameter was once passed to the method.

Parameter is Out of Bounds

Finally, let’s see what happens if the charAt() technique is passed a function fee that is out of bounds.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.charAt(85));

The following will be output to the net browser console log:

<empty string>

Because the first position in the string is 0, the role parameter ought to be a value between 0 and string.length-1. If the role parameter is out of bounds and does no longer fall in this range, the charAt() approach will return an empty string.

Since eighty five is a position that is out of bounds for the string ‘TechOnTheNet’, the charAt() technique back an empty string in the above example.