Moon
Moon

Reputation: 20012

Explanation of Code - I think it's Lambda expression

It's been long since i have been a programmer but i sometimes i just ask stupid questions. I have not some new techniques which make me pretty old fashioned programmer. For example the following code, is to remove all cookies.

var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
    var cookie = cookies[i];
    var eqPos = cookie.indexOf('=');
    var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
    document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT';
}

In there it is doing this

eqPos > -1 ? cookie.substr(0, eqPos) : cookie;

i don't know what is happening here. Could you please explain. And what is this type of notation and what do i need to learn to understand it.

Upvotes: 1

Views: 50

Answers (1)

AMember
AMember

Reputation: 3057

it is a short if statment, this is the condition

eqPos > -1 

name will be:

cookie.substr(0, eqPos)

if the condition is true:

and

cookie

If it is false

Upvotes: 1

Related Questions