w3resource

JavaScript index Property: Array Object

Description

The index property is a read-only property. It contains the position of a regular expression match within a string.

Version

Implemented in JavaScript 1.2

Syntax

array.index

Example:

In the following web document a regular expression string that contains the search string 'fox'. The match() method is used to find the regular expression within the string 'str1'. The index property is used to get the position of the word 'fox' within the string 'str1'

HTML Code

<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf8" />
<title>JavaScript Array object - index Property example</title>
<style type="text/css">h1 {color:red}
</style></head>
<body>
<h1>JavaScript Array Object : index property</h1>
<script src="array-index-example1.js"></script>
</body>
</html>

JS Code

var mymatch='fox';
var str1= "The quick brown fox jumps over the lazy dog.";
var myArray = str1.match(mymatch);

var newParagraph = document.createElement("p");
var newText = document.createTextNode('Original String : '+str1); 
newParagraph.appendChild(newText);
document.body.appendChild(newParagraph); 
var newParagraph1 = document.createElement("p"); 
var newText1 = document.createTextNode("The word 'fox' was found at position : "+myArray.index); 
newParagraph1.appendChild(newText1);
document.body.appendChild(newParagraph1);

View the example in the browser

Practice the example online

JavaScript Array object - index Property example
See also:

JavaScript Core objects, methods, properties.

Previous: JavaScript constructor Property: Array Object
Next: JavaScript input Property: Array Object

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/javascript/object-property-method/array-index.php