# Element Text Set To 0 Equals Empty

**URL:** <https://forum.playcanvas.com/t/element-text-set-to-0-equals-empty/13811>\
**Category:** Help & Support\
**Created:** [June 29, 2020, 12:50am UTC](https://forum.playcanvas.com/t/element-text-set-to-0-equals-empty/13811 "2020-06-29T00:50:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TingleyWJ](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/tingleywj/32/8266_2.png) [@TingleyWJ](https://forum.playcanvas.com/u/TingleyWJ)\
**Post date:** [June 29, 2020, 12:50am UTC](https://forum.playcanvas.com/t/element-text-set-to-0-equals-empty/13811/1 "2020-06-29T00:50:38Z")

</div>

Hi all,

This is a simple issue with a simple fix, but I’m confused as to why it’s happening.

If you set a text element’s text to 0 via:

```javascript
this.entity.element.text = 0;

```

the text element sets its text to `""` and prints nothing.

The simple fix would be to change it to

```javascript
this.entity.element.text = "" + 0;

```

but it’s strange that this is occurring in the first place.

My guess, which might be completely off, is that internally there’s a check of the same kind as `0 == ''` which evaluates to true thanks to js’s strange rules.

---

<div class="post-metadata">

**Author:** ![DevPlex01](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/devplex01/32/11123_2.png) [@DevPlex01](https://forum.playcanvas.com/u/DevPlex01)\
**Post date:** [June 29, 2020, 1:34am UTC](https://forum.playcanvas.com/t/element-text-set-to-0-equals-empty/13811/2 "2020-06-29T01:34:56Z")

</div>

If you want it to print 0, you would put the quotes around the 0. 0 on its own is registered as null, and thus it won’t show anything (this happens because texts register strings only and not numbers, it’ll register 0 as null because 0 means nothing or null). If you want it to print 0, you need to have it in quotations for it to register the 0 as a string.

---

<div class="post-metadata">

**Author:** ![saif](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/saif/32/35457_2.png) [@saif](https://forum.playcanvas.com/u/saif)\
**Post date:** [June 29, 2020, 4:19am UTC](https://forum.playcanvas.com/t/element-text-set-to-0-equals-empty/13811/3 "2020-06-29T04:19:37Z")

</div>

You can use toString method to print 0. You can change your line to

```javascript
this.entity.element.text = (0).toString();

```

---

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [June 29, 2020, 7:18am UTC](https://forum.playcanvas.com/t/element-text-set-to-0-equals-empty/13811/4 "2020-06-29T07:18:21Z")

</div>

> [@TingleyWJ](#):
>
> this.entity.element.text = 0;

Hi @TingleyWJ,

That happens because the engine internally will convert all values passed to the text property to string:

> <https://github.com/playcanvas/engine/blob/main/src/framework/components/element/text-element.js#L1212>

So yes, you should take care of passing only string values to this property to avoid getting unexpected behaviour.

---

<div class="post-metadata">

**Author:** ![LeXXik](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/lexxik/32/6953_2.png) [@LeXXik](https://forum.playcanvas.com/u/LeXXik)\
**Post date:** [June 29, 2020, 8:45am UTC](https://forum.playcanvas.com/t/element-text-set-to-0-equals-empty/13811/5 "2020-06-29T08:45:10Z")

</div>

I met the issue in my leaderboards at some point, when I noticed some players had no score. As you said, @TingleyWJ, it was easy to fix, so I started to explicitly convert my scores to string before assigning to the element property.

@Leonidas pointed to the sources of the engine, where the check happens. As you assumed, it is due to the way JS evaluates zeros:

```javascript
var val = 0;
if (val) {} // false

```

However, I do agree, that this is not intuitive. If I am able to do:

```javascript
this.entity.element.text = 7;

```

Then I should be able to assign a zero as well. You can consider creating an issue in the [engine repo](https://github.com/playcanvas/engine/issues).
