How are strings physically stored in Javascript - Stack Overflow

What I am looking for is how strings are physically treated in Javascript. Best example I can think of

What I am looking for is how strings are physically treated in Javascript. Best example I can think of for what I mean is that in the Java api it describes the storage of strings as:

String str = "abc";" is equivalent to: "char data[] = {'a', 'b', 'c'};

To me this says it uses an array object and stores each character as its own object to be used/accessed later (I am usually wrong on these things!)...

How does Javascript do this?

What I am looking for is how strings are physically treated in Javascript. Best example I can think of for what I mean is that in the Java api it describes the storage of strings as:

String str = "abc";" is equivalent to: "char data[] = {'a', 'b', 'c'};

To me this says it uses an array object and stores each character as its own object to be used/accessed later (I am usually wrong on these things!)...

How does Javascript do this?

Share Improve this question edited Mar 1, 2013 at 22:48 Ash Burlaczenko 25.5k16 gold badges69 silver badges101 bronze badges asked Mar 1, 2013 at 22:46 user1360809user1360809 7452 gold badges13 silver badges25 bronze badges 2
  • 1 it bees a String object, however, you can get characters as an array as well: var str = "Hello"; console.log(str[0]); //shows "H" – kennypu Commented Mar 1, 2013 at 22:50
  • they are "physically" stored, they are digitally stored obviously. while you can use the array access [] to get individual characters , strings are not arrays or char in javascript there is no char type and string doesnt have the methods related to array manipulation. – mpm Commented Mar 1, 2013 at 22:58
Add a ment  | 

2 Answers 2

Reset to default 3

Strings are String objects in JavaScript. The String object can use the [] notation to get character from a string ("abc"[0] returns 'a'). You can also use the String.prototype.charAt function to achieve the same result.

Side node: var a = 'abc' and var b = new String('abc') are not the same. The first case is called a primitive string and get converted to a String object by the JavaScript parser. This results in other data types, calling typeof(a) gives you string but typeof(b) gives you object.

Strings are stored in the same format in javascript as other languages stores. Suppose var word = "test" than at word will be as an array of characters and the 't' will e at 0th position and so on.

The last iteration as taking 'word.length' will return undefined. In other languages, it returns as '\0'.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745115230a4612087.html

相关推荐

  • How are strings physically stored in Javascript - Stack Overflow

    What I am looking for is how strings are physically treated in Javascript. Best example I can think of

    7小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信