[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt [Back]  [Original]

String.prototype.charCodeAt() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

String.prototype.charCodeAt()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..

charCodeAt() ( , 0x10000).

In this article

str.charCodeAt(index)

index

, 0 ; , 0.

0 1114111 (0x10FFFF). 128 ASCII. JavaScript.

, charCodeAt() , 65536. , ( ) -, . , 65536 , charCodeAt(i), charCodeAt(i + 1) ( ). .

charCodeAt() NaN, .

: (, JavaScript 1.2) charCodeAt() ISO-Latin-1 . ISO-Latin-1 0 255. 127 ASCII.

: charCodeAt()

65, Unicode A.

js
"ABC".charCodeAt(0); //  65

: charCodeAt() ,

for, , -.

js
function fixedCharCodeAt(str, idx) {
  // , fixedCharCodeAt('\uD800\uDC00', 0); // 65536
  // , fixedCharCodeAt('\uD800\uDC00', 1); // false
  idx = idx || 0;
  var code = str.charCodeAt(idx);
  var hi, low;

  //     (     0xDB7F,
  //          
  //  )
  if (0xd800 <= code && code <= 0xdbff) {
    hi = code;
    low = str.charCodeAt(idx + 1);
    if (isNaN(low)) {
      throw "        fixedCharCodeAt()";
    }
    return (hi - 0xd800) * 0x400 + (low - 0xdc00) + 0x10000;
  }
  if (0xdc00 <= code && code <= 0xdfff) {
    //    
    //   false,     ,
    //   
    //     
    return false;
    // hi = str.charCodeAt(idx - 1);
    // low = code;
    // return ((hi - 0xD800) * 0x400) +
    //   (low - 0xDC00) + 0x10000;
  }
  return code;
}

: charCodeAt() ,

js
function knownCharCodeAt(str, idx) {
  str += "";
  var code,
    end = str.length;

  var surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
  while (surrogatePairs.exec(str) != null) {
    var li = surrogatePairs.lastIndex;
    if (li - 2 < idx) {
      idx++;
    } else {
      break;
    }
  }

  if (idx >= end || idx < 0) {
    return NaN;
  }

  code = str.charCodeAt(idx);

  var hi, low;
  if (0xd800 <= code && code <= 0xdbff) {
    hi = code;
    low = str.charCodeAt(idx + 1);
    //   ,       
    return (hi - 0xd800) * 0x400 + (low - 0xdc00) + 0x10000;
  }
  return code;
}

Specification
ECMAScript 2027 LanguageSpecification
# sec-string.prototype.charcodeat


Web Proxy Viewer  |  New URL  |  Original Page