search

Home  >  Q&A  >  body text

In JS, does 'a' occupy 2 bytes or 1 byte in memory?

1. In JS, does 'a' occupy 2 bytes or 1 byte in memory? JS is encoded in UCS-2, so it should take up 2 bytes, right?

2. Are JS strings encoded the same in different engines?

3. In JS, how does a string represent the "end" in memory? Is it also necessary to add a \0?

世界只因有你世界只因有你2697 days ago1172

reply all(2)I'll reply

  • 某草草

    某草草2017-07-05 10:48:22

    String global object
    Read the documentation first~

    reply
    0
  • PHP中文网

    PHP中文网2017-07-05 10:48:22

    Let me answer what I know:

    1) The a letter (the first lowercase one in English) takes up two bytes. JS uses UCS-2, but it was later compatible with UTF16.

    But some rare words may not necessarily be 2 bytes in length, but may be multiple. For relevant knowledge, you can find Ruan Yifeng’s explanation of string knowledge. It is also recommended to read it

    Look at the relevant entries in the Wiki and refer to the reference links below (mainly documents from standardization organizations).

    2) The encoding is stipulated by ECMA and is the same. As a scripting language, its encoding is determined by the ECMA document.

    3) This is related to the engine.

    You can refer to the reply to this question

    https://www.zhihu.com/questio...

    reply
    0
  • Cancelreply