JavaScript String charCodeAt()"

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

charCodeAt() method in javascript that returns the integer representing the Unicode value of the character whose position in the string is passed as the argument of this method.

Syntax of String.prototype.charCodeAt() in JavaScript

The syntax of the charCodeAt() method in javascript is:

Here, str is the string, and index represents the position of the character in the string whose Unicode encoding is required.

The index of the first character is 0, the second character is 1, etc. The index of the last character is string size - 1.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Parameters of charCodeAt() in JavaScript

The charCodeAt() method accepts only one argument which is the index of the character in the string whose Unicode encoding is required. The index can range from 0 to string length - 1.

Return Value of String.prototype.charCodeAt() in JavaScript

The charCodeAt() method returns the character's Unicode, which ranges from 0 to 65535. This method returns NaN (Not-a-Number) if the argument passed is outside the range of the string which is 0 to string length - 1.

Exceptions of charCodeAt() in JavaScript

The charCodeAt() method return NaN (Not-a-Number) when the index provided as the argument lies outside the range of the string length i.e. if the index is negative or the index is greater than or equal to the string length.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Example

Output :

Explanation:

charCodeAt() extracts the character present at position 6 i.e. it extracts 'W' and then prints the Unicode integer of 'W' which is 87.

More Examples

  • When the index passed is valid i.e. it lies in the range of the string length which is 0 to string length - 1, then charcodeat javascript method return its unicode encoding.

Output :

Explanation :

charCodeAt() extracts the character present at position 4 i.e it extracts 'o' and then it prints the Unicode integer of 'o' which is 111.

  • When the index passed is not valid i.e. it lies outside the range of the string length i.e. if the index is negative or the index is greater than or equal to string length, then charcodeat JavaScript method returns NaN.

Output :

Explanation:

Since the index passed i.e. 12 is greater than or equals string length which is not a valid index therefore charcodeat javascript method returns NaN.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

Supported Browsers

All accepted browsers:

  • Google Chrome 1 and higher versions.
  • Edge 12 and higher versions.
  • using Firefox 1 and higher versions.
  • Safari 1 and higher versions.
  • Internet Explorer 4 and higher versions.
  • Opera 4 and higher versions.

Conclusion

  • Unicode is an information technology standard that is used to encode characters into integers.
  • charcodeat javascript method returns a character's Unicode whose index is passed as the argument to this method.
  • charcodeat javascript method returns NaN if the index passed is not valid i.e. if the index is negative or the index is greater than or equal to string length.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more