How can you check if a character is a Unicode character in JavaScript?

Is there a way to determine if a string contain Unicode characters using JavaScript? I know that JavaScript has a charCodeAt() method for getting the Unicode value of a character, but I’m not sure if it works for all Unicode characters. Is there a better way to do this?

To check if a character is a Unicode character in JavaScript, you can use the charCodeAt() method. This method returns the Unicode value of the character at the specified index in the string.

let str = "Hello, world!";
let charCode = str.charCodeAt(0);

if (charCode > 0xFF) {
  console.log("The character is a Unicode character.");
}

You can also use the codePointAt() method, which returns the Unicode code point value of the character at the specified index in the string. This method handles characters with code points greater than 0xFFFF, which are not representable in the charCodeAt() method.

let str = "Hello, world!";
let codePoint = str.codePointAt(0);

if (codePoint > 0xFF) {
  console.log("The character is a Unicode character.");
} 

You can use a regular expression (regex) to check if a string contains Unicode characters in JavaScript. The following regular expression checks for any character that is a Unicode character.

let str = "Hello, world!";
if ( /[^\u0000-\u00ff]/.test( str ) ) {
    console.log("The string contains a Unicode character.");
}