JavaScript Port Of PHP’s in_array()

The in_array() func­tion searches an array (haystack) for (needle) and returns false if it is found, false oth­er­wise. If the third para­meter strict is set to true then the in_array() func­tion will also check the types of the needle in the haystack.

function in_array(needle, haystack, strict)
{
	for(var i = 0, hLength = haystack.length, strict = !!strict; i < hLength; i++)
	{
		if((!strict && needle == haystack[i]) || (strict && needle === haystack[i]))
		{
			return true;
		}
	}

	return false;
}

It’s really simple and even con­verts strict to boolean if it isn’t already (strict = !!strict).

Comments

No responses have been made so far. Add your comments.

    What do you think? Leave a comment...