# JavaScript code problem with toString()

**URL:** <https://forum.makecode.com/t/javascript-code-problem-with-tostring/3864>\
**Category:** PXT\
**Created:** [October 12, 2020, 8:45pm UTC](https://forum.makecode.com/t/javascript-code-problem-with-tostring/3864 "2020-10-12T20:45:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![keble6](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@keble6](https://forum.makecode.com/u/keble6)\
**Post date:** [October 12, 2020, 8:45pm UTC](https://forum.makecode.com/t/javascript-code-problem-with-tostring/3864/1 "2020-10-12T20:45:27Z")

</div>

I wanted to use toString(radix) where radix is, say, 2 to display a number in binary.

But the JS window brings up this error: “Expected 0 arguments but got 1”  
 ![Screenshot 2020-10-12 at 21.42.36](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/b/b880aa118c1b65b28b4517474e6035e415d08182.png)

Is that a bug?

---

<div class="post-metadata">

**Author:** ![UnsignedArduino](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/unsignedarduino/32/592_2.png) [@UnsignedArduino](https://forum.makecode.com/u/UnsignedArduino)\
**Post date:** [October 12, 2020, 11:49pm UTC](https://forum.makecode.com/t/javascript-code-problem-with-tostring/3864/2 "2020-10-12T23:49:35Z")

</div>

Well, according to w3schools that’s not a bug.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/8/8338553535bf0610ae00ce2e15258f30bb2eb9fd.png)

> **[JavaScript Number toString() Method](https://www.w3schools.com/jsref/jsref_tostring_number.asp)**

If it’s like that in JavaScript, I’m guessing it would be the same for Typescript too, since TS is based on JS.

In your example, I’m guessing `astring` would be `"3"` if you didn’t pass in any parameters to `toString`.

---

<div class="post-metadata">

**Author:** ![kwx](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/kwx/32/1545_2.png) [@kwx](https://forum.makecode.com/u/kwx)\
**Post date:** [October 14, 2020, 1:57am UTC](https://forum.makecode.com/t/javascript-code-problem-with-tostring/3864/3 "2020-10-14T01:57:16Z")

</div>

This should normally work, toString is supposed to take an optional “radix” argument (2 for binary, 16 for hexadecimal): [https://github.com/microsoft/TypeScript/blob/master/src/lib/es5.d.ts#L522](https://github.com/microsoft/TypeScript/blob/master/src/lib/es5.d.ts#L522)

```auto
interface Number {
    /**
     * Returns a string representation of an object.
     * @param radix Specifies a radix for converting numeric values to strings. This value is only used for numbers.
     */
    toString(radix?: number): string;

```

However, MakeCode’s [Static TypeScript](https://makecode.com/language) doesn’t appear to understand that and produces the unexpected argument error.

---

<div class="post-metadata">

**Author:** ![kwx](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/kwx/32/1545_2.png) [@kwx](https://forum.makecode.com/u/kwx)\
**Post date:** [October 14, 2020, 2:05am UTC](https://forum.makecode.com/t/javascript-code-problem-with-tostring/3864/4 "2020-10-14T02:05:37Z")

</div>

Here’s a quick and inefficient hack, as long as you’re OK with only using it on positive integers:

```auto
function bitString (v: number) {
    let out = ""
    let bit = 1
    while (v > 0) {
        out = ((v & bit) ? "1" : "0") + out
        v &= ~bit
        bit *= 2
    }
    return out
}

let a = 244
console.log(bitString(a))

```

---

<div class="post-metadata">

**Author:** ![UnsignedArduino](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/unsignedarduino/32/592_2.png) [@UnsignedArduino](https://forum.makecode.com/u/UnsignedArduino)\
**Post date:** [October 14, 2020, 2:17am UTC](https://forum.makecode.com/t/javascript-code-problem-with-tostring/3864/5 "2020-10-14T02:17:46Z")

</div>

Ah, messed up again. _Facepalms_

But with a little digging around in the PXT repo, found this:

 ![Screenshot_20201013-221415](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/9/91a309cd61d6708c1fe766e0c609ce610f31639b.png)

> <https://github.com/microsoft/pxt/blob/master/libs/pxt-common/pxt-core.d.ts#L461>

Looks like it doesn’t take any arguments, unless I’m looking in the wrong spot. (Probably the latter 😅)
