{"id":312,"date":"2011-12-07T14:29:54","date_gmt":"2011-12-07T14:29:54","guid":{"rendered":"http:\/\/javascript-tutor.net\/index.php\/"},"modified":"2017-11-16T21:38:31","modified_gmt":"2017-11-16T21:38:31","slug":"lesson-7conditional-operators","status":"publish","type":"page","link":"https:\/\/javascript-tutor.net\/index.php\/lesson-7conditional-operators\/","title":{"rendered":"JavaScript Lesson 7:Conditional Operators"},"content":{"rendered":"<h4 align=\"center\"><a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-6-comparison-logical-operators\/\">&lt;Lesson 6&gt;<\/a> <a href=\"http:\/\/javascript-tutor.net\/index.php\/tutorial\/\">[Contents]<\/a> <a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-8-looping\/\">&lt;Lesson 8&gt;<\/a><\/h4>\n<p>In this lesson, we will\u00a0learn how to use conditional operators without using If and Else. Conditional\u00a0operators\u00a0let JavaScript\u00a0program executes certain jobs according to certain conditions.<\/p>\n<p>The syntax of the conditional operator is<\/p>\n<p><strong>Conditional\u00a0Expression ? Execution 1: Execution 2<\/strong><\/p>\n<p>The conditional operator comprises three parts, the first part is the conditional expression, the second part is execution 1 and the last part is execution 2. The question mark ? separates the conditional expression and execution 1 whilst the colon: separates execution 1 and execution 2. If the conditional expression is true, then JavaScript will run execution 1 and ignores execution2. However, if the conditional expression is false, then JavaScript will ignore execution 1 and run execution 2. Therefore, the symbol ? and : completely replace If and Else.<br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\"\n     style=\"display:block; text-align:center;\"\n     data-ad-layout=\"in-article\"\n     data-ad-format=\"fluid\"\n     data-ad-client=\"ca-pub-3033628290023372\"\n     data-ad-slot=\"5669011038\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h4><strong>Example\u00a07.1 <\/strong><\/h4>\n<p>In this example, since the expression x&gt;y is false, JavaScript executes the second action, i.e. to display 20 in the browser.<\/p>\n<pre style=\"font-size: 110%;\">&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=windows-1252\"&gt;\r\n&lt;title&gt;New Page 1&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;script&gt;\r\nx=10\r\ny=20\r\nx&gt;y?document.write(x):document.write(y)\r\n\r\n&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n<\/pre>\n<p>Click on <a href=\"http:\/\/javascript-tutor.net\/java_tutor\/Javascrpt_Example7.1.htm\">Example 7.1<\/a> to test it out.<\/p>\n<p>The above example can be rewritten\u00a0to make it more interactive by prompting the user to enter the values, as shown in Example 7.2<\/p>\n<h4><strong>Example 7.2<\/strong><\/h4>\n<pre style=\"font-size: 110%;\">&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=windows-1252\"&gt;\r\n&lt;title&gt;New Page 1&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;script&gt;\r\nx=window.prompt(\"Enter first value\",0)\r\ny=window.prompt(\"Enter second value\",0)\r\nx&gt;y?document.write(x):document.write(y)\r\n\r\n&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n<\/pre>\n<p>Click on\u00a0<a href=\"http:\/\/javascript-tutor.net\/java_tutor\/Javascrpt_Example7.2.htm\">Example 7.2 <\/a>to test it out.<br \/>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\"\n     style=\"display:block; text-align:center;\"\n     data-ad-layout=\"in-article\"\n     data-ad-format=\"fluid\"\n     data-ad-client=\"ca-pub-3033628290023372\"\n     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-6-comparison-logical-operators\/\">&lt;Lesson 6&gt;<\/a> <a href=\"http:\/\/javascript-tutor.net\/index.php\/tutorial\/\">[Contents]<\/a> <a href=\"http:\/\/javascript-tutor.net\/index.php\/lesson-8-looping\/\">&lt;Lesson 8&gt;<\/a><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>&lt;Lesson 6&gt; [Contents] &lt;Lesson 8&gt; In this lesson, we will\u00a0learn how to use conditional operators without using If and Else. Conditional\u00a0operators\u00a0let JavaScript\u00a0program executes certain jobs according to certain conditions. The syntax of the conditional operator is Conditional\u00a0Expression ? Execution 1: Execution 2 The conditional operator comprises three parts, the first part is the conditional expression, &hellip; <a href=\"https:\/\/javascript-tutor.net\/index.php\/lesson-7conditional-operators\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;JavaScript Lesson 7:Conditional Operators&#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-312","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>Conditional Operators in javascript<\/title>\n<meta name=\"description\" content=\"Learn how to use conditional operators 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-7conditional-operators\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Conditional Operators in javascript\" \/>\n<meta property=\"og:description\" content=\"Learn how to use conditional operators in javascript\" \/>\n<meta property=\"og:url\" content=\"https:\/\/javascript-tutor.net\/index.php\/lesson-7conditional-operators\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"article:modified_time\" content=\"2017-11-16T21:38:31+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-7conditional-operators\/\",\"url\":\"https:\/\/javascript-tutor.net\/index.php\/lesson-7conditional-operators\/\",\"name\":\"Conditional Operators in javascript\",\"isPartOf\":{\"@id\":\"https:\/\/javascript-tutor.net\/#website\"},\"datePublished\":\"2011-12-07T14:29:54+00:00\",\"dateModified\":\"2017-11-16T21:38:31+00:00\",\"description\":\"Learn how to use conditional operators in javascript\",\"breadcrumb\":{\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/lesson-7conditional-operators\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/javascript-tutor.net\/index.php\/lesson-7conditional-operators\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/lesson-7conditional-operators\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/javascript-tutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Lesson 7:Conditional Operators\"}]},{\"@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":"Conditional Operators in javascript","description":"Learn how to use conditional operators 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-7conditional-operators\/","og_locale":"en_US","og_type":"article","og_title":"Conditional Operators in javascript","og_description":"Learn how to use conditional operators in javascript","og_url":"https:\/\/javascript-tutor.net\/index.php\/lesson-7conditional-operators\/","og_site_name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","article_modified_time":"2017-11-16T21:38:31+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-7conditional-operators\/","url":"https:\/\/javascript-tutor.net\/index.php\/lesson-7conditional-operators\/","name":"Conditional Operators in javascript","isPartOf":{"@id":"https:\/\/javascript-tutor.net\/#website"},"datePublished":"2011-12-07T14:29:54+00:00","dateModified":"2017-11-16T21:38:31+00:00","description":"Learn how to use conditional operators in javascript","breadcrumb":{"@id":"https:\/\/javascript-tutor.net\/index.php\/lesson-7conditional-operators\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/javascript-tutor.net\/index.php\/lesson-7conditional-operators\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/javascript-tutor.net\/index.php\/lesson-7conditional-operators\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/javascript-tutor.net\/"},{"@type":"ListItem","position":2,"name":"JavaScript Lesson 7:Conditional Operators"}]},{"@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\/312","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=312"}],"version-history":[{"count":17,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/312\/revisions"}],"predecessor-version":[{"id":2476,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/312\/revisions\/2476"}],"wp:attachment":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}