{"id":398,"date":"2011-12-20T15:20:43","date_gmt":"2011-12-20T15:20:43","guid":{"rendered":"http:\/\/javascript-tutor.net\/index.php\/"},"modified":"2017-11-16T23:11:04","modified_gmt":"2017-11-16T23:11:04","slug":"lesson-26-performing-linear-search-array","status":"publish","type":"page","link":"https:\/\/javascript-tutor.net\/index.php\/lesson-26-performing-linear-search-array\/","title":{"rendered":"JavaScript Lesson 26: Performing Linear Search"},"content":{"rendered":"<h4 align=\"center\"><strong><a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-25-generating-random-values-array\/\">&lt;Lesson 25&gt;<\/a> <a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-tutorial\/\">[Contents]<\/a>\u00a0<a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-27-creating-cookies-in-javascript\/\">&lt;Lesson 27&gt;<\/a><\/strong><\/h4>\n<h3><strong>26.1 Introduction to Linear Search in JavaScript<\/strong><\/h3>\n<p>The ability to perform a search is one of the key functions of any programming languages. We don&#8217;t have to look very fast, just take a look at Google, the most popular search engine in the world. Due to its exclusive and highly effective search algorithm, it commands a major portion of the search engine market, and hence the search advertising market. In fact, it captured more than 60% of the Internet advertising market!<br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"5669011038\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><br \/>\nJavaScript is an effective scripting language that can be used to perform search operations on the web. off course, it has to work with server-side programming languages such as Java and CGI to be effective. However, you will learn simple search algorithm here using JavaScript.<\/p>\n<h3><strong>26.2\u00a0Performing Linear Search<\/strong><\/h3>\n<p>In this lesson, we will learn how to write a script to perform a linear search for an array that consists of integers. We employ the If operator to compare the search value entered by the user and each number in the array. If the search value is equal to the number concern, it will return the position of that number in the array (which is actually the value of the subscript plus 1, as the subscript of the array starts with 0). we need to use the function parseInt to convert the value of the subscript into a numeric value so that it can perform arithmetic operation. We also use the logical operators for and if to perform the search operation.<\/p>\n<p>The following is the script:<\/p>\n<pre>&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;title&gt;Performing Linear Search of an Array&lt;\/title&gt;&lt;script type = \"text\/javascript\"&gt;\r\n&lt;!--\r\nvar a = [4,8,13,17,19,22,25,35,39,41,49,53,59,67,69,75,78,83,90,99]\r\n\r\nfunction SearchArray()\r\n{\r\nvar searchNum = searchForm.inputVal.value;\r\n\r\nvar position = Search( a, parseInt( searchNum ) );\r\n\r\nif ( position != -1 )\r\nsearchForm.result.value =\r\n\"The number is in position \" +( parseInt(position)+1);\r\nelse\r\nsearchForm.result.value = \"The number is not found\";\r\n}\r\n\r\n\/\/ Search \"theArray\" for the specified \"key\" value\r\nfunction Search( theArray, key )\r\n{\r\nfor ( var n = 0; n &lt; theArray.length; ++n )\r\nif ( theArray[ n ] == key )\r\nreturn n;\r\n\r\nreturn -1;\r\n}\r\n\/\/ --&gt;\r\n&lt;\/script&gt;\r\n\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n&lt;h1&gt;Perfoming Linear Search&lt;\/h1&gt;\r\n&lt;form name = \"searchForm\" action = \"\"&gt;\r\n&lt;p&gt;Enter an Integer between 0 and 100 (excluding 0 and 100)&lt;br \/&gt;\r\n&lt;input name = \"inputVal\" type = \"text\" \/&gt;\r\n&lt;input name = \"search\" type = \"button\" value = \"Search\"\r\nonclick = \"SearchArray()\" \/&gt;&lt;br \/&gt;&lt;\/p&gt;\r\n\r\n&lt;p&gt;Result&lt;br \/&gt;\r\n&lt;input name = \"result\" type = \"text\" size = \"30\" \/&gt;&lt;\/p&gt;\r\n&lt;\/form&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n\r\n<\/pre>\n<p>Click\u00a0<a href=\"http:\/\/javascript-tutor.net\/java_tutor\/Javascrpt_Example26.1.htm\">Example 26.1 <\/a>to view the actual output.<br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"5669011038\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4 align=\"center\"><a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-25-generating-random-values-array\/\">&lt;Lesson 25&gt;<\/a> <a href=\"http:\/\/javascript-tutor.net\/index.php\/javascript-tutorial\/\">[Contents]<\/a>\u00a0<a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-27-creating-cookies-in-javascript\/\">&lt;Lesson 27&gt;<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>&lt;Lesson 25&gt; [Contents]\u00a0&lt;Lesson 27&gt; 26.1 Introduction to Linear Search in JavaScript The ability to perform a search is one of the key functions of any programming languages. We don&#8217;t have to look very fast, just take a look at Google, the most popular search engine in the world. Due to its exclusive and highly effective &hellip; <a href=\"https:\/\/javascript-tutor.net\/index.php\/lesson-26-performing-linear-search-array\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;JavaScript Lesson 26: Performing Linear Search&#8221;<\/span><\/a><\/p>\n","protected":false},"author":35895,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-398","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript Lesson 26: Performing Linear Search - Learn JavaScript Online | Free Interactive JavaScript Tutorials<\/title>\n<meta name=\"description\" content=\"This javascript lesson illustrates how to perform linear search in JavaScript\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/javascript-tutor.net\/index.php\/lesson-26-performing-linear-search-array\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Lesson 26: Performing Linear Search - Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"og:description\" content=\"This javascript lesson illustrates how to perform linear search in JavaScript\" \/>\n<meta property=\"og:url\" content=\"https:\/\/javascript-tutor.net\/index.php\/lesson-26-performing-linear-search-array\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"article:modified_time\" content=\"2017-11-16T23:11:04+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/lesson-26-performing-linear-search-array\/\",\"url\":\"https:\/\/javascript-tutor.net\/index.php\/lesson-26-performing-linear-search-array\/\",\"name\":\"JavaScript Lesson 26: Performing Linear Search - Learn JavaScript Online | Free Interactive JavaScript Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/javascript-tutor.net\/#website\"},\"datePublished\":\"2011-12-20T15:20:43+00:00\",\"dateModified\":\"2017-11-16T23:11:04+00:00\",\"description\":\"This javascript lesson illustrates how to perform linear search in JavaScript\",\"breadcrumb\":{\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/lesson-26-performing-linear-search-array\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/javascript-tutor.net\/index.php\/lesson-26-performing-linear-search-array\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/lesson-26-performing-linear-search-array\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/javascript-tutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Lesson 26: Performing Linear Search\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/javascript-tutor.net\/#website\",\"url\":\"https:\/\/javascript-tutor.net\/\",\"name\":\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\",\"description\":\"Master JavaScript with free, interactive tutorials for beginners and experienced coders. Practice live coding and visualize how JavaScript works step by step.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Lesson 26: Performing Linear Search - Learn JavaScript Online | Free Interactive JavaScript Tutorials","description":"This javascript lesson illustrates how to perform linear search in JavaScript","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/javascript-tutor.net\/index.php\/lesson-26-performing-linear-search-array\/","og_locale":"en_US","og_type":"article","og_title":"JavaScript Lesson 26: Performing Linear Search - Learn JavaScript Online | Free Interactive JavaScript Tutorials","og_description":"This javascript lesson illustrates how to perform linear search in JavaScript","og_url":"https:\/\/javascript-tutor.net\/index.php\/lesson-26-performing-linear-search-array\/","og_site_name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","article_modified_time":"2017-11-16T23:11:04+00:00","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/javascript-tutor.net\/index.php\/lesson-26-performing-linear-search-array\/","url":"https:\/\/javascript-tutor.net\/index.php\/lesson-26-performing-linear-search-array\/","name":"JavaScript Lesson 26: Performing Linear Search - Learn JavaScript Online | Free Interactive JavaScript Tutorials","isPartOf":{"@id":"https:\/\/javascript-tutor.net\/#website"},"datePublished":"2011-12-20T15:20:43+00:00","dateModified":"2017-11-16T23:11:04+00:00","description":"This javascript lesson illustrates how to perform linear search in JavaScript","breadcrumb":{"@id":"https:\/\/javascript-tutor.net\/index.php\/lesson-26-performing-linear-search-array\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/javascript-tutor.net\/index.php\/lesson-26-performing-linear-search-array\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/javascript-tutor.net\/index.php\/lesson-26-performing-linear-search-array\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/javascript-tutor.net\/"},{"@type":"ListItem","position":2,"name":"JavaScript Lesson 26: Performing Linear Search"}]},{"@type":"WebSite","@id":"https:\/\/javascript-tutor.net\/#website","url":"https:\/\/javascript-tutor.net\/","name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","description":"Master JavaScript with free, interactive tutorials for beginners and experienced coders. Practice live coding and visualize how JavaScript works step by step.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/users\/35895"}],"replies":[{"embeddable":true,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/comments?post=398"}],"version-history":[{"count":20,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/398\/revisions"}],"predecessor-version":[{"id":2505,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/398\/revisions\/2505"}],"wp:attachment":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}