One of the 2 conditions will most likely always be true.
Let's say email = 'paul@hotmail.com '; email.length equals 17.
email[16] equals ' ' so the email[k] != ' '
evaluates to false but we have not reached the length of email so k < email.length
is true; so, we will enter the loop.
email[17] equals null so email[k] != ' '
evaluates to true. Even though we have reached the end of our string and k < email.length
is false.
In other languages such as java you would get a IndexOutOfBounds exception for accessing an index in the array greater than the number of elements in the array. Javascript however allows you to do this and just returns null.
What you really want is to iterate only while both of the conditions are true, not one or the other.
var length = email.length;
for(var k = i+1; k < length && email[k] != ' '; k++)
This article on trimming a string may also be helpful/inciteful to you: http://blog.stevenlevithan.com/archives/faster-trim-javascript