# Need an if operation, don't know how to do

**URL:** <https://discourse.nodered.org/t/need-an-if-operation-dont-know-how-to-do/68887>\
**Category:** General\
**Created:** [11 October 2022 17:38 UTC](https://discourse.nodered.org/t/need-an-if-operation-dont-know-how-to-do/68887 "2022-10-11T17:38:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![igmam](https://avatars.discourse-cdn.com/v4/letter/i/3ec8ea/32.png) [@igmam](https://discourse.nodered.org/u/igmam)\
**Post date:** [11 October 2022 17:38 UTC](https://discourse.nodered.org/t/need-an-if-operation-dont-know-how-to-do/68887/1 "2022-10-11T17:38:31Z")

</div>

Hi there,

I'm doing a geolocation lookup via Google API.

I get the MAC Addresses proper, but i need a function wich will lookup if the address has 12 characters, if not, i need a "0" in front of my string.

Sorry for the noob question 🙂

---

<div class="post-metadata">

**Author:** ![madhouse](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@madhouse](https://discourse.nodered.org/u/madhouse)\
**Post date:** [11 October 2022 17:43 UTC](https://discourse.nodered.org/t/need-an-if-operation-dont-know-how-to-do/68887/2 "2022-10-11T17:43:18Z")

</div>

What do you have so far to do the processing? Not sure if I know what you're asking. Adding 0 to a MAC address or what?

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [11 October 2022 17:55 UTC](https://discourse.nodered.org/t/need-an-if-operation-dont-know-how-to-do/68887/3 "2022-10-11T17:55:08Z")

</div>

You do not need if, you can use padStart() or padEnd()

```auto
//str.padStart(targetLength, padString)
msg.payload = "12345678".padStart(9, "0");

```

If 9 characters in length no padding is added.

> **[String.prototype.padStart() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart)**
>
> The padStart() method pads the
> current string with another string (multiple times, if needed) until the resulting
> string reaches the given length. The padding is applied from the start of the
> current string.

---

<div class="post-metadata">

**Author:** ![igmam](https://avatars.discourse-cdn.com/v4/letter/i/3ec8ea/32.png) [@igmam](https://discourse.nodered.org/u/igmam)\
**Post date:** [11 October 2022 18:03 UTC](https://discourse.nodered.org/t/need-an-if-operation-dont-know-how-to-do/68887/4 "2022-10-11T18:03:26Z")

</div>

Hi there,

so my case would be:

msg.payload = "123456789as".padStart(11, "0");

?

Thanks!

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [11 October 2022 18:10 UTC](https://discourse.nodered.org/t/need-an-if-operation-dont-know-how-to-do/68887/5 "2022-10-11T18:10:13Z")

</div>

You could experiment to find out, but if you want a string 12 chars long then set `"12345678910".padStart(12,"0")`This should output "012345678910".  
Play around and try it.

---

<div class="post-metadata">

**Author:** ![igmam](https://avatars.discourse-cdn.com/v4/letter/i/3ec8ea/32.png) [@igmam](https://discourse.nodered.org/u/igmam)\
**Post date:** [11 October 2022 18:28 UTC](https://discourse.nodered.org/t/need-an-if-operation-dont-know-how-to-do/68887/6 "2022-10-11T18:28:31Z")

</div>

i did, it works already, thanks everyone!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [25 October 2022 18:28 UTC](https://discourse.nodered.org/t/need-an-if-operation-dont-know-how-to-do/68887/7 "2022-10-25T18:28:57Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
