# Why the string split result including the "undefined"?

**URL:** <https://discourse.nodered.org/t/why-the-string-split-result-including-the-undefined/14195>\
**Category:** General\
**Created:** [10 August 2019 15:20 UTC](https://discourse.nodered.org/t/why-the-string-split-result-including-the-undefined/14195 "2019-08-10T15:20:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![316248269](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/316248269/32/10773_2.png) [@316248269](https://discourse.nodered.org/u/316248269)\
**Post date:** [10 August 2019 15:20 UTC](https://discourse.nodered.org/t/why-the-string-split-result-including-the-undefined/14195/1 "2019-08-10T15:20:10Z")

</div>

Hi:  
I write this function to split a string.  
var D = msg.payload;  
var M = ;  
var j = 0;  
for(var i = 0; i \< D.length; i++){  
if(D[i]!='|')  
M[j] += D[i];  
else  
j++;  
}  
msg.payload = M;  
return msg;

For example, if I input 'ab|c', and I want to split the string to two values without '|', the output results are:  
array[2]

0: "undefinedab"

1: "undefinedc"  
The result is almost correct but just including the 'undefined'. Why?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [10 August 2019 15:34 UTC](https://discourse.nodered.org/t/why-the-string-split-result-including-the-undefined/14195/2 "2019-08-10T15:34:18Z")

</div>

You want to add `D[i]` to the array, but you are concatenating `+=` instead.  
Shouldn't you use `M[j].push(D[i])` instead ?

Note that there is a `split` node to do this stuff.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 August 2019 15:46 UTC](https://discourse.nodered.org/t/why-the-string-split-result-including-the-undefined/14195/3 "2019-08-10T15:46:41Z")

</div>

The reason is that the line  
`M[j] += D[i];`  
is the same as  
`M[j] = M[j] + D[i];`  
so consider what happens the first time through. This is  
`M[0] = M[0] + "a";`  
but as initially `M[0]` is undefined the result is "undefineda".  
You need to change

```auto
j++

```

to

```auto
j++
M[j] = ""

```

so that it is initialised to an empty string. However, as @bakman2 said there is a Split node that will do this for you.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 August 2019 19:59 UTC](https://discourse.nodered.org/t/why-the-string-split-result-including-the-undefined/14195/4 "2019-08-10T19:59:06Z")

</div>

In fact (as @zenofmud has pointed out to me) my solution is not quite correct, because the `j++` is in an `else` block, but is not surrounded by braces, it is necessary to include the braces. Personally I _never_ write code like

```auto
if (A) 
  B;
else
  C;

```

I would always write

```auto
If (A) {
  B;
} else {
  C;
}

```

because I have seen bugs introduced so many times when an additional line is inserted into the if or else clause.  
In addition it is necessary to initialise M[0] at the start. So a working version of the function is

```auto
var D = msg.payload;
var M = [];
var j = 0;
M[j] = ''; // < initialise M[0]
for(var i = 0; i < D.length; i++){
    if(D[i]!='|') {
        M[j] += D[i];
    } else{
        j++;
        M[j] = ''; // < initialise M[j]
    }
}
msg.payload = M;
return msg;

```

As before, though, a Split node will do the job with much less effort.
