Tutorialspoint.dev

JavaScript | string.codePointAt()

The string.codePointAt() is an inbuilt function in JavaScript which is used to return a non-negative integer value i.e, code point value of the specified element of the given string.
List of code point values of different elements:
Syntax:

string.codePointAt(A)

Parameters: It accepts a parameter which shows the index of an element in the string. Index starts form zero (0).
Return Values: It returns the code point value of the element which is denoted by parameter in the string. It returns undefined if there is no element present at the specified location i.e, at “A”th index.

JavaScript code to show the working of string.codePointAt() function:

Code #1:

<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
    document.write(b + "<br>")
    document.write(c + "<br>")
    document.write(d)
  
</script>

Output:

103
102
103

Code #2:

<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
    document.write(b + "<br>")
  
</script>

Output:

undefined

Note: Output is undefined because in “gfg” there is no element at 4th index.



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter