10.07.2017 Views

javascript_tutorial

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Javascript<br />

<br />

<br />

<br />

Output<br />

[0] is 12<br />

[1] is 5<br />

[2] is 8<br />

[3] is 130<br />

[4] is 44<br />

indexOf ()<br />

Javascript array indexOf() method returns the first index at which a given<br />

element can be found in the array, or -1 if it is not present.<br />

Syntax<br />

Its syntax is as follows:<br />

array.indexOf(searchElement[, fromIndex]);<br />

Parameter Details<br />

<br />

searchElement : Element to locate in the array.<br />

<br />

fromIndex : The index at which to begin the search. Defaults to 0, i.e.<br />

the whole array will be searched. If the index is greater than or equal to<br />

the length of the array, -1 is returned.<br />

Return Value<br />

Returns the index of the found element.<br />

Compatibility<br />

This method is a JavaScript extension to the ECMA-262 standard; as such it may<br />

not be present in other implementations of the standard. To make it work, you<br />

need to add the following code at the top of your script.<br />

if (!Array.prototype.indexOf)<br />

{<br />

Array.prototype.indexOf = function(elt /*, from*/)<br />

{<br />

191

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!