# Reversing String will add undefined

**URL:** <https://forum.makecode.com/t/reversing-string-will-add-undefined/45409>\
**Category:** Help\
**Tags:** graphics-and-math, extension\
**Created:** [August 15, 2026, 6:21pm UTC](https://forum.makecode.com/t/reversing-string-will-add-undefined/45409 "2026-08-15T18:21:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cybercube](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/cybercube/32/34628_2.png) [@Cybercube](https://forum.makecode.com/u/Cybercube)\
**Post date:** [August 15, 2026, 6:21pm UTC](https://forum.makecode.com/t/reversing-string-will-add-undefined/45409/1 "2026-08-15T18:21:14Z")

</div>

so, i was making a reverse string block but, every time i did, it adds undefined in it. here’s the code:

```auto
    //% block="Reverse $str"
    export function Reverse(str: string) {
        let list: string[] = []
        let s;
        for (let index = 0; index <= str.length; index++) {
            list.push(str.substr(index, 1))
        }
        list.reverse()
        for (let value of list) {
            s = s + value
        }
        return s;
    }

```

---

<div class="post-metadata">

**Author:** ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)\
**Post date:** [August 15, 2026, 6:22pm UTC](https://forum.makecode.com/t/reversing-string-will-add-undefined/45409/2 "2026-08-15T18:22:08Z")

</div>

`index` should iterate when it’s strictly less than `str.length`; _i.e._, get rid of the equals sign.

---

<div class="post-metadata">

**Author:** ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)\
**Post date:** [August 15, 2026, 6:25pm UTC](https://forum.makecode.com/t/reversing-string-will-add-undefined/45409/3 "2026-08-15T18:25:01Z")

</div>

Here’s a simpler version, as you can access individual characters in strings:

```typescript
function reverse(s: string): string {
    let toReturn: string = ""
    for (let i: number = s.length - 1; i >= 0; i--) {
        toReturn += s[i]
    }
    return toReturn
}

```
