I've recently picked up some javascript code - because I'm no js expert, once I get it running, I like to disect it so that I can better my understanding.
Here's the code:
function checkCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(";");
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == " ") c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
return null;
}
The bit that's confusing is the stacked while and if statement - if I add curly brackets, I get a clearer picture like this:
while (c.charAt(0) == " ") {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length, c.length);
}
But what I can't understand is why a while statement is being used at all - surely an 'if' would be more appropriate - or have I got the brackets wrong?
Clarification would be great.