How to Get Character of Specific Position using JavaScript ?

How to Get Character of Specific Position using JavaScript ?

At 6/7/2023

In this article, we will see how to get the character of a specific position using JavaScript. There are three methods to get the character at a specific position using JavaScript. These are:

  • Using String charAt() Method
  • Using String substr() Method
  • Using Square Bracket Notation

Using String charAt() Method: The charAt() method is used to get the character at a specific position in a string. This method takes the index as an argument and returns the character of the given index in the string.

Example:

Javascript

let str = "Welcome to Freesad";
let index = 11;
let ctr = str.charAt(index);
console.log("Character at " + index + "th position is: " + ctr);

Output:

Character at 11th position is: F

Using String substr() Method: The substr() method can also be used to get the character at a specific position in a string. This method returns the specified number of characters from a string, starting from the given index.

Example:

Javascript

let str = "Welcome to Freesad";
let index = 11;
let ctr = str.substr(index, 1);
console.log("Character at " + index + "th position is: " + ctr);

Output:

Character at 11th position is: F

Using Square Bracket Notation: The square bracket can also be used to get the character at a specific position in a string.

Example:

Javascript

let str = "Welcome to Freesad";
let index = 11;
let ctr = str[index];
console.log("Character at " + index + "th position is: " + ctr);

Output:

Character at 11th position is: F

حقوق التأليف والنشر

نحن نحترم حقوق الملكية للآخرين ونحرص دائمًا على عدم التعدي على حقوقهم، لذا يحق للمؤلفين ودور النشر المطالبة بإزالة رابط تحميل مقال أو كتاب من الموقع. إذا وجدت مقالاً أو كتاباً خاصاً بك ولم توافق على نشر رابط التحميل أو كان لديك اقتراح أو شكوى راسلنا عبر تواصل معنا, أو عن طريق البريد الإلكتروني في: support@freewsad.com.

المزيد عنا