JavaScript String codePointAt() Method
JavaScript string codePointAt() is an inbuilt method in JavaScript that is used to return a non-negative integer value i.e, the code point value of the specified element of the given string. that
Syntax:
string.codePointAt(A)
Parameters: It accepts a parameter that shows the index of an element in the string. The index starts from zero (0).
Return Values: It returns the code point value of the element which is denoted by a parameter in the string. It returns undefined if there is no element present at the specified location i.e, at “A”th index.
Below is an example of the string.codePointAt() Method.
Example: This example shows the basic use of the string.codePointAt() Method in javascript.
javascript
<script> a = "gfg" b = a.codePointAt(1); console.log(b) </script> |
Output:
102
JavaScript code to show the working of string.codePointAt() method:
Example 1: This example shows the basic use of the string.codePointAt() Method in javascript.
javascript
<script> // Taking a string "gfg" a = "gfg" // Pointing each elements of the string b = a.codePointAt(0); c = a.codePointAt(1); d = a.codePointAt(2); // Printing the code point value of each element console.log(b) console.log(c) console.log(d) </script> |
Output:
103 102 103
Example 2: The output of the example comes out to be undefined as the third index does not exist.
javascript
<script> // Taking a string "gfg" a = "gfg" // Pointing 4th index of the string // index starts from 0 b = a.codePointAt(3); // Printing the code point value console.log(b) </script> |
Output:
undefined
Note: Output is undefined because in “gfg” there is no element at 4th index.
We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.
Supported Browsers:
- Google Chrome 41 and above
- Edge 12 and above
- Firefox 29 and above
- Opera 28 and above
- Safari 10 and above
Please Login to comment...