Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

JavaScript CharAt

by suresh

JavaScript CharAt method returns the character at a specified index position. The JavaScript CharAt function index position will start from 0, Not 1. Let me show you the syntax of the JavaScript CharAt function

String_Object.CharAt(Index_Position)
  • String_Object: Please select the valid String Object.
  • Index_Position: Provide the index position of the desired character.

Return Value: ChartAt Function in Javascript returns the Character from String_Object at specified Index_Position. If we specify the Index position out of range, ChartAt Function returns an empty string.

JavaScript CharAt Example

The following set of examples will help you understand the JavaScript ChartAt Function.

<!DOCTYPE html>
<html>
<head>
    <title> JavaScript CharAt </title>
</head>
<body>
    <p id= "Content1">Content 1</p>
    <p id= "Content2">Content 3</p>
    <p id= "Content3">Content 2</p>
<script>
  var Str_Original = "Tutorial GateWay";
  var Str_Extracted = Str_Original.charAt(9);
  var Str_Extracted1 = Str_Original.charAt(Str_Original.length - 1);
  var Str_Extracted2 = Str_Original.charAt(Str_Original.length);
    
  document.getElementById("Content1").innerHTML = "Charcter at Index position 9 = " + Str_Extracted;
  document.getElementById("Content2").innerHTML = "Charcter at Index position 15 = " + Str_Extracted1; 
  document.getElementById("Content3").innerHTML = "Charcter at Index position 16 = " + Str_Extracted2; 
  
  document.write("Charcter at Index position 9 = " + Str_Extracted);
</script>
</body>
</html>
JavaScript CharAt

First, we declared the String variable and assigned a value using the following statement

var Str_Original = "Tutorial GateWay";

It will find the Character at index position 9, and it will be G.

var Str_Extracted = Str_Original.charAt(9);

NOTE: JavaScript charat function count the space as One Character.

In the next line, we are using the JavaScript len function to calculate the string length.

var Str_Extracted1 = Str_Original.charAt(Str_Original.length - 1);

In the above statement, we are subtracting one from string length because, length of a Str_Original is 16, and there is no character in the index position 16. Let us see the result when we choose the index position as 16

var Str_Extracted2 = Str_Original.charAt(Str_Original.length);

The above JavaScript Character At statement returns the empty string. The following statements will place the content in the respective paragraphs

document.getElementById("Content1").innerHTML = "Charcter at Index position 9 = " + Str_Extracted;
document.getElementById("Content2").innerHTML = "Charcter at Index position 15 = " + Str_Extracted1; 
document.getElementById("Content3").innerHTML = "Charcter at Index position 16 = " + Str_Extracted2;

Placed Under: JavaScript

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial

Copyright © 2021 · All Rights Reserved by Suresh

About Us | Contact Us | Privacy Policy