# Treating strings as an array of chars, maybe a new Char type?

**URL:** <https://es.discourse.group/t/treating-strings-as-an-array-of-chars-maybe-a-new-char-type/1393>\
**Category:** 💡 Ideas\
**Created:** [July 8, 2022, 8:44pm UTC](https://es.discourse.group/t/treating-strings-as-an-array-of-chars-maybe-a-new-char-type/1393 "2022-07-08T20:44:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ColtHands](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/colthands/32/1411_2.png) [@ColtHands](https://es.discourse.group/u/ColtHands)\
**Post date:** [July 8, 2022, 8:44pm UTC](https://es.discourse.group/t/treating-strings-as-an-array-of-chars-maybe-a-new-char-type/1393/1 "2022-07-08T20:44:46Z")

</div>

`String`'s are sort of a collection of `Char`'s, but immutable. So, why not make it more functional and iterative?

Right now, to treat string as an array we have to `.split('')` and then `.join('')` back, but what if this would happen more seamlessly?

You can sort of achieve it with this hacky hack:

`String.prototype.map = Array.prototype.map`

And then if you call

`"hello".map((e,i,self) => self[self.length-1-i])`

Which will return `['o', 'l', 'l', 'e', 'h']`.

This kinda works with methods that don't mutate the string (previously array). But still yeilds some weird behavior.

`for(a in str){ console.log(a) } // 0, 1, 2, 3, 4, 5, map, filter`

And will not with things that do

```nohighlight
String.prototype.reverse = Array.prototype.reverse
"hello".reverse()

```

Will yield and error saying that "we can assign value to readonly property".

Just imagine how amazing, clean and simple would JS be. Heck, we are going functional way, which is great, why not go all the way.

`someString.split('').sort().join('')`

will become

`someString.sort()`

and

`someString.split('').reverse().join('')`

will become

`someString.reverse()`

Is this even necessary? Are there any better ideas how to go about this?

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [July 8, 2022, 9:05pm UTC](https://es.discourse.group/t/treating-strings-as-an-array-of-chars-maybe-a-new-char-type/1393/2 "2022-07-08T21:05:01Z")

</div>

You don't have to do the horrifically bad practice of mutating objects you don't own to do that; you can `Array.prototype.map.call(string, mapper).join('')`.

What would be the benefit of a Char type of length 1, especially considering that 💩 has a length of 2, and 🏳️‍🌈 has a length of 6?

If you're talking about cases where you want to map one string to another one character at a time (but not one code point, or one grapheme cluster at a time); or use cases where you want to reverse a string by character (but not by code point, or by grapheme cluster, or if the text is RTL), then that would be interesting to hear more about.

---

<div class="post-metadata">

**Author:** ![nisabmohd](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nisabmohd/32/1942_2.png) [@nisabmohd](https://es.discourse.group/u/nisabmohd)\
**Post date:** [January 15, 2024, 7:35am UTC](https://es.discourse.group/t/treating-strings-as-an-array-of-chars-maybe-a-new-char-type/1393/3 "2024-01-15T07:35:25Z")

</div>

**May be a new Char type?**

Basically in JavaScript string is a primitive data type. And a type is primitive only when a data type cannot be further broken in primitive types.  
Suppose in Java String are not primitive because they can be broken down to char primitive type (in java we have a dedicated char type), hence String in Java cannot be primitive.

Where as in JavaScript a string is primitive because there is no such thing called char type in JavaScript, even we break down each element we refer to char is still string only. Therefore if we want a new char type in JavaScript then Strings will not be primitive whole idea needs to be changes from a language perspective.

---

<div class="post-metadata">

**Author:** ![ColtHands](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/colthands/32/1411_2.png) [@ColtHands](https://es.discourse.group/u/ColtHands)\
**Post date:** [May 8, 2025, 12:56pm UTC](https://es.discourse.group/t/treating-strings-as-an-array-of-chars-maybe-a-new-char-type/1393/6 "2025-05-08T12:56:43Z")

</div>

Continuing the discussion i believe the char primitive could be a good performance + pattern addition to the language, while we take best things from other languages (take mixins or F# pipes for example) i think char primitive is present in most modern languages.

@ljharb maybe you could point me to a process or a guide to make this proposal as most structured and orgnized as possible? possibly there's steps/guides etc?

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [May 8, 2025, 7:39pm UTC](https://es.discourse.group/t/treating-strings-as-an-array-of-chars-maybe-a-new-char-type/1393/7 "2025-05-08T19:39:38Z")

</div>

Browsers are likely not going to add new primitives ever, at this point, for performance reasons specifically.
