JavaScript charAt() Method: Method to get Character Based On Index Value.

0

In this blog post you’ll learn about the JavaScript charAt() method. Method to extract the character from its index value (position).


Description:

charAt() method returns the character from the string based on the index value given to it.


Syntax:

JavaScript


string.charAt(index);  


Parameter Description:

index: The index value (position) of character. Default position is 0. This parameter is optional.


Example 1: Get the character based on the index.


JavaScript


    let text = "Hello Good morning!. Welcome to maketechstuff.com";
    document.write(text.charAt());
    // Output
    // H

JavaScript


    let text = "Hello Good morning!. Welcome to maketechstuff.com";
    document.write(text.charAt(4));
    // Output
    // o


Example 2: Get the first and last character from the string.

Get the first and last character from the given string.


JavaScript


    let text = "Hello Good morning!. Welcome to maketechstuff.com";
    document.write("First character: ", text.charAt());
    document.write("<br>");
    document.write("Last character: ", text.charAt(text.length - 1));
    // Output:
    // First character: H
    // Last character: m
  


So that's how the JavaScript charAt() method works. If you have any query or suggestion feel free to write them in the comment section.




Tags

Post a Comment

0Comments

Share your thoughts.

Post a Comment (0)
To Top