JavaScript RangeError Argument is not a valid code point

This JavaScript exception Invalid code point occurs if NaN values, negative Integers, non-Integers, or other values larger than 0x10FFFF are used with the String.fromCodePoint() method.

Message:

RangeError: {0} is not a valid code point (Firefox)
RangeError: Invalid code point {0} (Chromium)

Error Type:

RangeError

Cause of the error: The String.fromCodePoint() is used to return a string created using the sequence of code points that are specified as parameters. It throws this error if the passed code point values are NaN values, negative Integers, non-Integers, or values larger than 0x10FFFF.

Example 1: This example works without throwing any error because the value passed to the method is valid.



HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <script src=
        </script>
    </head>
    <body style="text-align: center;">
        <h1 style="color: green;">
            GeeksforGeeks
        </h1>
  
        <p>
            JavaScript RangeError 
            Argument is not a valid code point
        </p>
  
        <button onclick="Geeks();">
            click here
        </button>
        <p id="GFG_DOWN"></p>
  
        <script>
            var el_down = document.getElementById("GFG_DOWN");
            function Geeks() {
                try {
                    String.fromCodePoint(34);
                    el_down.innerHTML = 
                      "'Argument is not a valid code point'" +
                      " error has not occurred";
                } catch (e) {
                    
                    // Show the error in console
                    console.log(e);
                    el_down.innerHTML = 
                      "'Argument is not a valid code point'" +
                      " error has occurred";
                }
            }
        </script>
    </body>
</html>

chevron_right


Output:

Example 2: In this example, the value passed to the method is NaN, which is an invalid value, therefore the error has occurred.

HTML

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <script src=
        </script>
    </head>
  
    <body style="text-align: center;">
        <h1 style="color: green;">
            GeeksforGeeks
        </h1>
  
        <p>
            JavaScript RangeError 
            Argument is not a valid code point
        </p>
  
        <button onclick="Geeks();">
            click here
        </button>
        <p id="GFG_DOWN"></p>
  
        <script>
            var el_down = document.getElementById("GFG_DOWN");
            function Geeks() {
                try {
                    String.fromCodePoint(NaN);
                    el_down.innerHTML = 
                      "'Argument is not a valid code point'" +
                      " error has not occurred";
                } catch (e) {
                    // Show the error in console
                    console.log(e);
                    el_down.innerHTML = 
                      "'Argument is not a valid code point'" +
                      " error has occurred";
                }
            }
        </script>
    </body>
</html>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.