/    Sign up×
Community /Pin to ProfileBookmark

understanding if check in simple inArray method

hellow guys , have a look at the below function , which checks for an element in an array ::

[CODE] inArray: function( elem, arr, i ) {
var len;

if ( arr ) {
if ( indexOf ) {
return indexOf.call( arr, elem, i );
}

len = arr.length;
i = i ? i < 0 ? Math.max( 0, len + i ) : i : 0;

for ( ; i < len; i++ ) {
// Skip accessing in sparse arrays
if ( i in arr && arr[ i ] === elem ) {
return i;
}
}
}

return -1;
},[/CODE]

can somebody tell me what is this if check for below in the above fucntion ::

[CODE]if ( indexOf ) {[/CODE]

what is this check really doing ?

Thank you.

Gautam.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERJun 20.2015 — It is checking to see if the function "indexOf" is supported by the browser.

If it is supported, then the 'true' part uses the supported function to search the array.

If it is not supported, then it creates it's own version to be used on the sparse array passed

to see if a particular element is "InArray".
Copy linkTweet thisAlerts:
@gautamz07authorJun 20.2015 — if i try a simple test in the console , like so ::

[CODE]if (indexOf) {
console.log('yes');
}[/CODE]


i get an error , see below ::


[CODE]/*
Exception: ReferenceError: indexOf is not defined
@Scratchpad/1:10:1
*/[/CODE]


Why ?
Copy linkTweet thisAlerts:
@iBeZiJun 21.2015 — Is indexOf being set anywhere, i.e. I'd expect there to be something like this somewhere.

<i>
</i>var indexOf = [].indexOf;


This seems like a really strange way of doing things though, I'm pretty sure indexOf is universally supported, I'm not sure why you'd need to check if it exists.
Copy linkTweet thisAlerts:
@xelawhoJun 21.2015 — indexOf was originally only available for strings. Modern browsers made it available as an array method years ago. The latest versions of IE have just started doing the same. Considering how many people are still using IE8, it's not a bad idea to check
Copy linkTweet thisAlerts:
@iBeZiJun 21.2015 — I did not know that, I'd kind of hope most people would be moving away from ie8 and below now so hopefully it shouldn't be that big of an issue... still, handy to know, thanks
Copy linkTweet thisAlerts:
@xelawhoJun 21.2015 — You would hope that they are moving away from IE period, but if you look at the stats, IE8 is still widely used and worth accounting for
Copy linkTweet thisAlerts:
@gautamz07authorJun 22.2015 — Xel thanks for the info , and ibzi , your 1st answer is correct , i figured out later , btw , this function is from jquery source .
×

Success!

Help @gautamz07 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...