Array reduce explanation

I would like to get some advise through javascript cookies handling.
I have these code for extracting values from cookies.However i understand all the way till .map from .reduce array method i have some doubt. Can any body tell me what is happening through these method. Especially in


this line.And also why i need to use decodeuri.


 let cookieString="firstName=john;   lastName=Doe;password =1234";
                const getCookies1=cookieString
                document.write(cookieString + `<br>`);

  I need some explanation.

>Solution :

I can only hope you are not serious about storing passwords in cookies and that this ist just an example string.

But what the code does is interpreting the string as semicolon-separated key/value pairs where each key is taken from the left hand-side of the ‘=’ symbol without leading and trailing whitespaces (that’s what trim() does) and the value from the right hand-side of the ‘=’ symbol, expecting the value to be possibly URI encoded.

Nobody knows why the value is not also trimmed. Imagine the string firstName = John; lastName = Doe;. Then getCokies1.firstName would be John (mind the leading space).

Leave a Reply