CINXE.COM

<!DOCTYPE html><html lang="en" data-theme-enabled="1"><head><script>window.currentUser = null;</script><script>window.shopCurrency = "EUR";</script><script>window.localCurrency = "EUR";</script><script>window.countryCode = "us";</script><script>window.rateShopTo = {"EUR":1,"USD":1.0807994889980017,"AMD":422.5677915267482};</script><title itemprop="name">Logical operators</title><link href="/pack/styles.c582d23a0695e653a6d3.css" rel="stylesheet"><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes, minimum-scale=1.0"><meta name="apple-mobile-web-app-capable" content="yes"><script>if (window.devicePixelRatio > 1) document.cookie = 'pixelRatio=' + window.devicePixelRatio + ';path=/;expires=Tue, 19 Jan 2038 03:14:07 GMT';</script><link href="//fonts.googleapis.com/css?family=Open+Sans:bold,italic,bolditalic" rel="stylesheet"><link rel="apple-touch-icon-precomposed" href="/img/favicon/apple-touch-icon-precomposed.png"><link rel="canonical" href="https://javascript.info/logical-operators"><meta name="msapplication-TileColor" content="#222A2C"><meta name="msapplication-TileImage" content="/img/favicon/tileicon.png"><link rel="icon" href="/img/favicon/favicon.png"><meta itemprop="image" content="https://javascript.info/img/site_preview_en_512x512.png"><meta property="og:title" content="Logical operators"><meta property="og:image" content="https://javascript.info/img/site_preview_en_1200x630.png"><meta property="og:image:type" content="image/png"><meta property="og:image:width" content="1200"><meta property="og:image:height" content="630"><meta property="fb:admins" content="100001562528165"><meta name="twitter:card" content="summary"><meta name="twitter:title" content="Logical operators"><meta name="twitter:site" content="@iliakan"><meta name="twitter:creator" content="@iliakan"><meta name="twitter:image" content="https://javascript.info/img/site_preview_en_512x512.png"><meta name="google-adsense-account" content="ca-pub-6204518652652613"><link rel="prev" href="/ifelse"><link rel="next" href="/nullish-coalescing-operator"><script data-collect-dnt="true" async src="https://scripts.simpleanalyticscdn.com/latest.js"></script><script>window.GA_ID = "UA-2056213-15";</script><script>window.YANDEX_METRIKA_ID = 32184394;</script><script>{function gtag(){dataLayer.push(arguments)}window.dataLayer=window.dataLayer||[],gtag("js",new Date),gtag("config","G-2LWB61WGYJ")}</script> <script async src="https://www.googletagmanager.com/gtag/js?id=G-2LWB61WGYJ"></script><script>window.metrika={reachGoal:function(){}},window.yandex_metrika_callbacks=[function(){try{window.metrika=new Ya.Metrika({id:YANDEX_METRIKA_ID,webvisor:!0,clickmap:!0,params:{user:window.currentUser&&window.currentUser.id}}),metrika.trackLinks({delay:150}),window.addEventListener("error",function(r){window.metrika.reachGoal("JSERROR",{src:(r.filename||r.errorUrl)+": "+(r.lineno||r.errorLine),stack:r.stack||r.error&&r.error.stack,message:r.message})})}catch(r){}}];</script><script src="//mc.yandex.ru/metrika/watch.js" async></script><script>window.RECAPTCHA_ID = "6LfmLAEVAAAAAJMykMnf7aY8nkyTRmYi2ynx51R1";</script><script src="/pack/init.c620e89b5f96f3cade19.js"></script><script src="/pack/head.7de24b11bdf2e7982166.js" defer></script><meta property="og:title" content="Logical operators"><meta property="og:type" content="article"><script src="/pack/tutorial.aa68ac65c8ad26a37344.js" defer></script><script src="/pack/footer.818a809d6860f4026867.js" defer></script></head><body class="no-icons"><script>window.fontTest();</script><div class="page-wrapper page-wrapper_sidebar_on"><!--[if IE]><div style="color:red;text-align:center">Sorry, Internet Explorer is not supported, please use a newer browser.</div><![endif]--><div class="sitetoolbar sitetoolbar_tutorial"><script>window.langs = [{"code":"ar","name":"Arabic"},{"code":"az","name":"Azerbaijani"},{"code":"bg","name":"Bulgarian"},{"code":"bn","name":"Bengali"},{"code":"bs","name":"Bosnian"},{"code":"ca","name":"Catalan"},{"code":"cs","name":"Czech"},{"code":"da","name":"Danish"},{"code":"de","name":"German"},{"code":"el","name":"Greek"},{"code":"en","name":"English"},{"code":"es","name":"Spanish"},{"code":"fa","name":"Persian (Farsi)"},{"code":"fi","name":"Finnish"},{"code":"fr","name":"French"},{"code":"he","name":"Hebrew"},{"code":"hi","name":"Hindi"},{"code":"hr","name":"Croatian"},{"code":"hu","name":"Hungarian"},{"code":"hy","name":"Armenian"},{"code":"id","name":"Indonesian"},{"code":"it","name":"Italian"},{"code":"ja","name":"Japanese"},{"code":"ka","name":"Georgian"},{"code":"kk","name":"Kazakh"},{"code":"km","name":"Central Khmer"},{"code":"ko","name":"Korean"},{"code":"ku","name":"Kurdish"},{"code":"ky","name":"Kyrgyz"},{"code":"lt","name":"Lithuanian"},{"code":"me","name":"Montenegrin"},{"code":"ml","name":"Malayalam"},{"code":"ms","name":"Malay"},{"code":"my","name":"Burmese"},{"code":"nl","name":"Dutch"},{"code":"no","name":"Norvegian"},{"code":"pa","name":"Punjabi"},{"code":"pl","name":"Polish"},{"code":"pt","name":"Portuguese"},{"code":"ro","name":"Romanian"},{"code":"ru","name":"Russian"},{"code":"si","name":"Sinhala"},{"code":"sk","name":"Slovak"},{"code":"sl","name":"Slovenian"},{"code":"sq","name":"Albanian"},{"code":"sr","name":"Serbian"},{"code":"ta","name":"Tamil"},{"code":"te","name":"Telugu"},{"code":"test","name":"Test"},{"code":"th","name":"Thai"},{"code":"tk","name":"Turkmen"},{"code":"tr","name":"Turkish"},{"code":"ug","name":"Uyghur"},{"code":"uk","name":"Ukrainian"},{"code":"ur","name":"Urdu"},{"code":"uz","name":"Uzbek"},{"code":"v2","name":"v2"},{"code":"vi","name":"Vietnamese"},{"code":"zh-hant","name":"Chinese Traditional"},{"code":"zh","name":"Chinese"}];</script><script>window.lang = "en";</script><div class="sitetoolbar__content"><div class="sitetoolbar__lang-switcher"><button class="sitetoolbar__dropdown-button" data-dropdown-toggler>EN</button><div class="sitetoolbar__dropdown-wrap"><div class="sitetoolbar__dropdown-body"><div class="sitetoolbar__lang-switcher-body"><div class="supported-langs supported-langs_toolbar"><div class="supported-langs__container"><ul class="supported-langs__list" style="height:200px"><li class="supported-langs__item"><a class="supported-langs__link" href="https://ar.javascript.info/logical-operators"><span class="supported-langs__brief">AR</span><span class="supported-langs__title">عربي</span></a></li><li class="supported-langs__item supported-langs__item_current"><a class="supported-langs__link" href="https://javascript.info/logical-operators"><span class="supported-langs__brief">EN</span><span class="supported-langs__title">English</span></a></li><li class="supported-langs__item"><a class="supported-langs__link" href="https://es.javascript.info/logical-operators"><span class="supported-langs__brief">ES</span><span class="supported-langs__title">Español</span></a></li><li class="supported-langs__item"><a class="supported-langs__link" href="https://fa.javascript.info/logical-operators"><span class="supported-langs__brief">FA</span><span class="supported-langs__title">فارسی</span></a></li><li class="supported-langs__item"><a class="supported-langs__link" href="https://fr.javascript.info/logical-operators"><span class="supported-langs__brief">FR</span><span class="supported-langs__title">Français</span></a></li><li class="supported-langs__item"><a class="supported-langs__link" href="https://id.javascript.info/logical-operators"><span class="supported-langs__brief">ID</span><span class="supported-langs__title">Indonesia</span></a></li></ul><ul class="supported-langs__list" style="height:200px"><li class="supported-langs__item"><a class="supported-langs__link" href="https://it.javascript.info/logical-operators"><span class="supported-langs__brief">IT</span><span class="supported-langs__title">Italiano</span></a></li><li class="supported-langs__item"><a class="supported-langs__link" href="https://ja.javascript.info/logical-operators"><span class="supported-langs__brief">JA</span><span class="supported-langs__title">日本語</span></a></li><li class="supported-langs__item"><a class="supported-langs__link" href="https://ko.javascript.info/logical-operators"><span class="supported-langs__brief">KO</span><span class="supported-langs__title">한국어</span></a></li><li class="supported-langs__item"><a class="supported-langs__link" href="https://learn.javascript.ru/logical-operators"><span class="supported-langs__brief">RU</span><span class="supported-langs__title">Русский</span></a></li><li class="supported-langs__item"><a class="supported-langs__link" href="https://tr.javascript.info/logical-operators"><span class="supported-langs__brief">TR</span><span class="supported-langs__title">Türkçe</span></a></li><li class="supported-langs__item"><a class="supported-langs__link" href="https://uk.javascript.info/logical-operators"><span class="supported-langs__brief">UK</span><span class="supported-langs__title">Українська</span></a></li></ul><ul class="supported-langs__list" style="height:20px"><li class="supported-langs__item"><a class="supported-langs__link" href="https://zh.javascript.info/logical-operators"><span class="supported-langs__brief">ZH</span><span class="supported-langs__title">简体中文</span></a></li></ul></div><div class="supported-langs__text"><p>We want to make this open-source project available for people all around the world.</p> <p><a href="https://javascript.info/translate">Help to translate</a> the content of this tutorial to your language!</p> </div></div></div></div></div></div><div class="sitetoolbar__logo-wrap"><a class="sitetoolbar__link sitetoolbar__link_logo" href="/"><img class="sitetoolbar__logo sitetoolbar__logo_normal" src="/img/sitetoolbar__logo_en.svg" width="200" alt="" role="presentation"/><img class="sitetoolbar__logo sitetoolbar__logo_normal sitetoolbar__logo_dark" src="/img/sitetoolbar__logo_en-white.svg" width="200" alt="" role="presentation"/><img class="sitetoolbar__logo sitetoolbar__logo_small" src="/img/sitetoolbar__logo_small_en.svg" width="70" alt="" role="presentation"/><img class="sitetoolbar__logo sitetoolbar__logo_small sitetoolbar__logo_dark" src="/img/sitetoolbar__logo_small_en-white.svg" width="70" alt="" role="presentation"/><script>Array.prototype.forEach.call(document.querySelectorAll("img.sitetoolbar__logo"),function(e){let t=document.createElement("object");t.type="image/svg+xml",t.className=e.className,t.style.cssText="left:0;top:0;position:absolute",t.onload=function(){t.onload=null,e.style.visibility="hidden"},t.data=e.src,e.parentNode.insertBefore(t,e)});</script></a></div><div class="sitetoolbar__nav-toggle-wrap"><button class="sitetoolbar__nav-toggle" type="button"></button></div><nav class="sitetoolbar__sections"><ul class="sitetoolbar__sections-list"></ul></nav><div class="sitetoolbar__right-button-wrap"><a class="sitetoolbar-right-button sitetoolbar-right-button_courses" href="/ebook"><span class="sitetoolbar-right-button__extra-text">Buy</span>EPUB/PDF</a></div><div class="sitetoolbar__login-wrap"><button class="sitetoolbar__login sitetoolbar__login_unready" data-action-login></button></div><div class="sitetoolbar__theme-switcher"><div class="theme-changer"><label class="theme-changer__label" for="theme-changer-input" data-tooltip="Change theme"><input class="theme-changer__input" type="checkbox" id="theme-changer-input" data-theme-changer="data-theme-changer"/><span class="theme-changer__icon theme-changer__icon_light-theme"></span><span class="theme-changer__icon theme-changer__icon_dark-theme"></span></label></div></div><div class="sitetoolbar__search-wrap"><div class="sitetoolbar__search-content"><button class="sitetoolbar__search-toggle" type="button"></button><form class="sitetoolbar__search" method="GET" action="/search"><div class="sitetoolbar__search-input"><div class="text-input"><input class="text-input__control" name="query" placeholder="Search on Javascript.info" required="required" type="text"/></div><button class="sitetoolbar__find" type="submit">Search</button></div></form></div></div></div><div class="tablet-menu"><div class="tablet-menu__line"><div class="tablet-menu__content"><form class="tablet-menu-search" action="/search/"><input class="tablet-menu-search__input" type="search" name="query" placeholder="Search in the tutorial" required="required"/><button class="tablet-menu-search__button" type="submit" name="type" value="articles">Search</button></form></div></div><div class="tablet-menu__line"><div class="tablet-menu__content"><a class="map" href="/tutorial/map" data-action="tutorial-map"><span class="map__text">Tutorial map</span></a></div></div><div class="tablet-menu__line"><div class="tablet-menu__content"><div class="theme-changer theme-changer_tablet-menu theme-changer_has-label"><label class="theme-changer__label" for="theme-changer-input-tablet" data-tooltip="Change theme"><input class="theme-changer__input" type="checkbox" id="theme-changer-input-tablet" data-theme-changer="data-theme-changer"/><span class="theme-changer__icon theme-changer__icon_light-theme"></span><span class="theme-changer__icon theme-changer__icon_dark-theme"></span><span class="theme-changer__label-text theme-changer__label-text_light-theme">Light theme</span><span class="theme-changer__label-text theme-changer__label-text_dark-theme">Dark theme</span></label></div></div></div><div class="tablet-menu__line"><div class="tablet-menu__content"><div class="share-icons"><span class="share-icons__title">Share</span><a class="share share_tw" href="https://twitter.com/share?url=https%3A%2F%2Fjavascript.info%2Flogical-operators" rel="nofollow"></a><a class="share share_fb" href="https://www.facebook.com/sharer/sharer.php?s=100&amp;p%5Burl%5D=https%3A%2F%2Fjavascript.info%2Flogical-operators" rel="nofollow"></a></div></div></div><div class="tablet-menu__line"><div class="tablet-menu__content"><select class="tablet-menu__nav input-select input-select input-select_small" onchange="if(this.value) window.location.href=this.value"><option value="https://ar.javascript.info/logical-operators">عربي</option><option value="https://javascript.info/logical-operators" selected>English</option><option value="https://es.javascript.info/logical-operators">Español</option><option value="https://fa.javascript.info/logical-operators">فارسی</option><option value="https://fr.javascript.info/logical-operators">Français</option><option value="https://id.javascript.info/logical-operators">Indonesia</option><option value="https://it.javascript.info/logical-operators">Italiano</option><option value="https://ja.javascript.info/logical-operators">日本語</option><option value="https://ko.javascript.info/logical-operators">한국어</option><option value="https://learn.javascript.ru/logical-operators">Русский</option><option value="https://tr.javascript.info/logical-operators">Türkçe</option><option value="https://uk.javascript.info/logical-operators">Українська</option><option value="https://zh.javascript.info/logical-operators">简体中文</option></select></div></div></div><progress class="tutorial-progress" data-sticky value="15" max="94" data-tooltip="Lesson 15 of 94"></progress></div><div class="page page_sidebar_on page_inner_padding"><script>if(localStorage.noSidebar){document.querySelector(".page").classList.remove("page_sidebar_on");let e=document.querySelector(".page-wrapper");e&&e.classList.remove("page-wrapper_sidebar_on")}setTimeout(function(){document.querySelector(".page").classList.add("page_sidebar-animation-on")});</script><div class="page__inner"><main class="main main_width-limit"><header class="main__header"><div class="main__header-inner"><div class="main__header-group"><ol class="breadcrumbs"><li class="breadcrumbs__item breadcrumbs__item_home"><a class="breadcrumbs__link" href="/"><span class="breadcrumbs__hidden-text">Tutorial</span></a></li><li class="breadcrumbs__item" id="breadcrumb-1"><a class="breadcrumbs__link" href="/js"><span>The JavaScript language</span></a></li><li class="breadcrumbs__item" id="breadcrumb-2"><a class="breadcrumbs__link" href="/first-steps"><span>JavaScript Fundamentals</span></a></li><script type="application/ld+json">{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Tutorial","item":"https://javascript.info/"},{"@type":"ListItem","position":2,"name":"The JavaScript language","item":"https://javascript.info/js"},{"@type":"ListItem","position":3,"name":"JavaScript Fundamentals","item":"https://javascript.info/first-steps"}]}</script></ol><div class="updated-at" data-tooltip="Last updated on June 5, 2022"><div class="updated-at__content">June 5, 2022</div></div></div><h1 class="main__header-title">Logical operators</h1></div></header><div class="content"><article class="formatted" itemscope itemtype="http://schema.org/TechArticle"><meta itemprop="name" content="Logical operators"><div itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="email" content="iliakan@gmail.com"><meta itemprop="name" content="Ilya Kantor"></div><div itemprop="articleBody"><p>There are four logical operators in JavaScript: <code>||</code> (OR), <code>&amp;&amp;</code> (AND), <code>!</code> (NOT), <code>??</code> (Nullish Coalescing). Here we cover the first three, the <code>??</code> operator is in the next article.</p> <p>Although they are called “logical”, they can be applied to values of any type, not only boolean. Their result can also be of any type.</p> <p>Let’s see the details.</p> <h2><a class="main__anchor" name="or" href="#or">|| (OR)</a></h2><p>The “OR” operator is represented with two vertical line symbols:</p> <div id="js2uewne2f" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>result = a || b;</code></pre> </div> </div> </div><p>In classical programming, the logical OR is meant to manipulate boolean values only. If any of its arguments are <code>true</code>, it returns <code>true</code>, otherwise it returns <code>false</code>.</p> <p>In JavaScript, the operator is a little bit trickier and more powerful. But first, let’s see what happens with boolean values.</p> <p>There are four possible logical combinations:</p> <div id="rjpu1ug005" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( true || true ); // true alert( false || true ); // true alert( true || false ); // true alert( false || false ); // false</code></pre> </div> </div> </div><p>As we can see, the result is always <code>true</code> except for the case when both operands are <code>false</code>.</p> <p>If an operand is not a boolean, it’s converted to a boolean for the evaluation.</p> <p>For instance, the number <code>1</code> is treated as <code>true</code>, the number <code>0</code> as <code>false</code>:</p> <div id="ealafcjvvv" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>if (1 || 0) { // works just like if( true || false ) alert( 'truthy!' ); }</code></pre> </div> </div> </div><p>Most of the time, OR <code>||</code> is used in an <code>if</code> statement to test if <em>any</em> of the given conditions is <code>true</code>.</p> <p>For example:</p> <div id="5wuafby8im" data-trusted="1" class="code-example" data-highlight="[{&quot;start&quot;:2,&quot;end&quot;:2}]"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>let hour = 9; if (hour &lt; 10 || hour &gt; 18) { alert( 'The office is closed.' ); }</code></pre> </div> </div> </div><p>We can pass more conditions:</p> <div id="h5c4tkrgpw" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>let hour = 12; let isWeekend = true; if (hour &lt; 10 || hour &gt; 18 || isWeekend) { alert( 'The office is closed.' ); // it is the weekend }</code></pre> </div> </div> </div><h2><a class="main__anchor" name="or-finds-the-first-truthy-value" href="#or-finds-the-first-truthy-value">OR &quot;||&quot; finds the first truthy value</a></h2><p>The logic described above is somewhat classical. Now, let’s bring in the “extra” features of JavaScript.</p> <p>The extended algorithm works as follows.</p> <p>Given multiple OR’ed values:</p> <div id="k44nnidcjp" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>result = value1 || value2 || value3;</code></pre> </div> </div> </div><p>The OR <code>||</code> operator does the following:</p> <ul> <li>Evaluates operands from left to right.</li> <li>For each operand, converts it to boolean. If the result is <code>true</code>, stops and returns the original value of that operand.</li> <li>If all operands have been evaluated (i.e. all were <code>false</code>), returns the last operand.</li> </ul> <p>A value is returned in its original form, without the conversion.</p> <p>In other words, a chain of OR <code>||</code> returns the first truthy value or the last one if no truthy value is found.</p> <p>For instance:</p> <div id="83iroqhnch" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( 1 || 0 ); // 1 (1 is truthy) alert( null || 1 ); // 1 (1 is the first truthy value) alert( null || 0 || 1 ); // 1 (the first truthy value) alert( undefined || null || 0 ); // 0 (all falsy, returns the last value)</code></pre> </div> </div> </div><p>This leads to some interesting usage compared to a “pure, classical, boolean-only OR”.</p> <ol> <li> <p><strong>Getting the first truthy value from a list of variables or expressions.</strong></p> <p>For instance, we have <code>firstName</code>, <code>lastName</code> and <code>nickName</code> variables, all optional (i.e. can be undefined or have falsy values).</p> <p>Let’s use OR <code>||</code> to choose the one that has the data and show it (or <code>&quot;Anonymous&quot;</code> if nothing set):</p> <div id="1yeonw5qjv" data-trusted="1" class="code-example" data-highlight="[{&quot;start&quot;:4,&quot;end&quot;:4}]"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>let firstName = &quot;&quot;; let lastName = &quot;&quot;; let nickName = &quot;SuperCoder&quot;; alert( firstName || lastName || nickName || &quot;Anonymous&quot;); // SuperCoder</code></pre> </div> </div> </div><p>If all variables were falsy, <code>&quot;Anonymous&quot;</code> would show up.</p> </li> <li> <p><strong>Short-circuit evaluation.</strong></p> <p>Another feature of OR <code>||</code> operator is the so-called “short-circuit” evaluation.</p> <p>It means that <code>||</code> processes its arguments until the first truthy value is reached, and then the value is returned immediately, without even touching the other argument.</p> <p>The importance of this feature becomes obvious if an operand isn’t just a value, but an expression with a side effect, such as a variable assignment or a function call.</p> <p>In the example below, only the second message is printed:</p> <div id="hh54w473zu" data-trusted="1" class="code-example" data-highlight="[{&quot;start&quot;:1,&quot;cols&quot;:[{&quot;start&quot;:0,&quot;end&quot;:5}]},{&quot;start&quot;:0,&quot;cols&quot;:[{&quot;start&quot;:0,&quot;end&quot;:4}]}]"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>true || alert(&quot;not printed&quot;); false || alert(&quot;printed&quot;);</code></pre> </div> </div> </div><p>In the first line, the OR <code>||</code> operator stops the evaluation immediately upon seeing <code>true</code>, so the <code>alert</code> isn’t run.</p> <p>Sometimes, people use this feature to execute commands only if the condition on the left part is falsy.</p> </li> </ol> <h2><a class="main__anchor" name="and" href="#and">&amp;&amp; (AND)</a></h2><p>The AND operator is represented with two ampersands <code>&amp;&amp;</code>:</p> <div id="u9us02zwjf" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>result = a &amp;&amp; b;</code></pre> </div> </div> </div><p>In classical programming, AND returns <code>true</code> if both operands are truthy and <code>false</code> otherwise:</p> <div id="b52t1mru88" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( true &amp;&amp; true ); // true alert( false &amp;&amp; true ); // false alert( true &amp;&amp; false ); // false alert( false &amp;&amp; false ); // false</code></pre> </div> </div> </div><p>An example with <code>if</code>:</p> <div id="pyhmzv45l6" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>let hour = 12; let minute = 30; if (hour == 12 &amp;&amp; minute == 30) { alert( 'The time is 12:30' ); }</code></pre> </div> </div> </div><p>Just as with OR, any value is allowed as an operand of AND:</p> <div id="beydfmzn33" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>if (1 &amp;&amp; 0) { // evaluated as true &amp;&amp; false alert( &quot;won't work, because the result is falsy&quot; ); }</code></pre> </div> </div> </div><h2><a class="main__anchor" name="and-finds-the-first-falsy-value" href="#and-finds-the-first-falsy-value">AND “&amp;&amp;” finds the first falsy value</a></h2><p>Given multiple AND’ed values:</p> <div id="mytvnqkiej" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>result = value1 &amp;&amp; value2 &amp;&amp; value3;</code></pre> </div> </div> </div><p>The AND <code>&amp;&amp;</code> operator does the following:</p> <ul> <li>Evaluates operands from left to right.</li> <li>For each operand, converts it to a boolean. If the result is <code>false</code>, stops and returns the original value of that operand.</li> <li>If all operands have been evaluated (i.e. all were truthy), returns the last operand.</li> </ul> <p>In other words, AND returns the first falsy value or the last value if none were found.</p> <p>The rules above are similar to OR. The difference is that AND returns the first <em>falsy</em> value while OR returns the first <em>truthy</em> one.</p> <p>Examples:</p> <div id="oaul9lmmt4" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>// if the first operand is truthy, // AND returns the second operand: alert( 1 &amp;&amp; 0 ); // 0 alert( 1 &amp;&amp; 5 ); // 5 // if the first operand is falsy, // AND returns it. The second operand is ignored alert( null &amp;&amp; 5 ); // null alert( 0 &amp;&amp; &quot;no matter what&quot; ); // 0</code></pre> </div> </div> </div><p>We can also pass several values in a row. See how the first falsy one is returned:</p> <div id="rwtqc8z4hr" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( 1 &amp;&amp; 2 &amp;&amp; null &amp;&amp; 3 ); // null</code></pre> </div> </div> </div><p>When all values are truthy, the last value is returned:</p> <div id="1eb80ph4ug" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( 1 &amp;&amp; 2 &amp;&amp; 3 ); // 3, the last one</code></pre> </div> </div> </div><div class="important important_smart"> <div class="important__header"><span class="important__type">Precedence of AND <code>&amp;&amp;</code> is higher than OR <code>||</code></span></div> <div class="important__content"><p>The precedence of AND <code>&amp;&amp;</code> operator is higher than OR <code>||</code>.</p> <p>So the code <code>a &amp;&amp; b || c &amp;&amp; d</code> is essentially the same as if the <code>&amp;&amp;</code> expressions were in parentheses: <code>(a &amp;&amp; b) || (c &amp;&amp; d)</code>.</p> </div></div> <div class="important important_warn"> <div class="important__header"><span class="important__type">Don’t replace <code>if</code> with <code>||</code> or <code>&amp;&amp;</code></span></div> <div class="important__content"><p>Sometimes, people use the AND <code>&amp;&amp;</code> operator as a “shorter way to write <code>if</code>”.</p> <p>For instance:</p> <div id="80z2ztglsk" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>let x = 1; (x &gt; 0) &amp;&amp; alert( 'Greater than zero!' );</code></pre> </div> </div> </div><p>The action in the right part of <code>&amp;&amp;</code> would execute only if the evaluation reaches it. That is, only if <code>(x &gt; 0)</code> is true.</p> <p>So we basically have an analogue for:</p> <div id="so7leozjlz" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>let x = 1; if (x &gt; 0) alert( 'Greater than zero!' );</code></pre> </div> </div> </div><p>Although, the variant with <code>&amp;&amp;</code> appears shorter, <code>if</code> is more obvious and tends to be a little bit more readable. So we recommend using every construct for its purpose: use <code>if</code> if we want <code>if</code> and use <code>&amp;&amp;</code> if we want AND.</p> </div></div> <h2><a class="main__anchor" name="not" href="#not">! (NOT)</a></h2><p>The boolean NOT operator is represented with an exclamation sign <code>!</code>.</p> <p>The syntax is pretty simple:</p> <div id="brha4zya9g" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>result = !value;</code></pre> </div> </div> </div><p>The operator accepts a single argument and does the following:</p> <ol> <li>Converts the operand to boolean type: <code>true/false</code>.</li> <li>Returns the inverse value.</li> </ol> <p>For instance:</p> <div id="c2ebqd6dr1" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( !true ); // false alert( !0 ); // true</code></pre> </div> </div> </div><p>A double NOT <code>!!</code> is sometimes used for converting a value to boolean type:</p> <div id="2g5uhjrvj9" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( !!&quot;non-empty string&quot; ); // true alert( !!null ); // false</code></pre> </div> </div> </div><p>That is, the first NOT converts the value to boolean and returns the inverse, and the second NOT inverses it again. In the end, we have a plain value-to-boolean conversion.</p> <p>There’s a little more verbose way to do the same thing – a built-in <code>Boolean</code> function:</p> <div id="ox0ektx9r3" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( Boolean(&quot;non-empty string&quot;) ); // true alert( Boolean(null) ); // false</code></pre> </div> </div> </div><p>The precedence of NOT <code>!</code> is the highest of all logical operators, so it always executes first, before <code>&amp;&amp;</code> or <code>||</code>.</p> </div></article><div class="tasks formatted"><h2 class="tasks__title" id="tasks"><a class="tasks__title-anchor main__anchor main__anchor main__anchor_noicon" href="#tasks">Tasks</a></h2><div class="task tasks__task"><div class="task__header"><div class="task__title-wrap"><h3 class="task__title"><a class="main__anchor" href="#what-s-the-result-of-or" name="what-s-the-result-of-or">What's the result of OR?</a></h3><a class="task__open-link" href="/task/alert-null-2-undefined" target="_blank"></a></div><div class="task__header-note"><span class="task__importance" title="How important is the task, from 1 to 5">importance: 5</span></div><div class="task__content"><div class="task__formatted"><p>What is the code below going to output?</p> <div id="9yxk8p8sv6" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( null || 2 || undefined );</code></pre> </div> </div> </div></div><button class="task__solution" type="button">solution</button><div class="task__answer"><div class="task__answer-content"><div class="formatted"><p>The answer is <code>2</code>, that’s the first truthy value.</p> <div id="a4h6wkosyr" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( null || 2 || undefined );</code></pre> </div> </div> </div></div></div><button class="close-button task__answer-close" type="button" title="close"></button></div></div></div></div><div class="task tasks__task"><div class="task__header"><div class="task__title-wrap"><h3 class="task__title"><a class="main__anchor" href="#what-s-the-result-of-or-ed-alerts" name="what-s-the-result-of-or-ed-alerts">What's the result of OR'ed alerts?</a></h3><a class="task__open-link" href="/task/alert-or" target="_blank"></a></div><div class="task__header-note"><span class="task__importance" title="How important is the task, from 1 to 5">importance: 3</span></div><div class="task__content"><div class="task__formatted"><p>What will the code below output?</p> <div id="isq1w1azuk" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( alert(1) || 2 || alert(3) );</code></pre> </div> </div> </div></div><button class="task__solution" type="button">solution</button><div class="task__answer"><div class="task__answer-content"><div class="formatted"><p>The answer: first <code>1</code>, then <code>2</code>.</p> <div id="eflnn88ico" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( alert(1) || 2 || alert(3) );</code></pre> </div> </div> </div><p>The call to <code>alert</code> does not return a value. Or, in other words, it returns <code>undefined</code>.</p> <ol> <li>The first OR <code>||</code> evaluates its left operand <code>alert(1)</code>. That shows the first message with <code>1</code>.</li> <li>The <code>alert</code> returns <code>undefined</code>, so OR goes on to the second operand searching for a truthy value.</li> <li>The second operand <code>2</code> is truthy, so the execution is halted, <code>2</code> is returned and then shown by the outer alert.</li> </ol> <p>There will be no <code>3</code>, because the evaluation does not reach <code>alert(3)</code>.</p> </div></div><button class="close-button task__answer-close" type="button" title="close"></button></div></div></div></div><div class="task tasks__task"><div class="task__header"><div class="task__title-wrap"><h3 class="task__title"><a class="main__anchor" href="#what-is-the-result-of-and" name="what-is-the-result-of-and">What is the result of AND?</a></h3><a class="task__open-link" href="/task/alert-1-null-2" target="_blank"></a></div><div class="task__header-note"><span class="task__importance" title="How important is the task, from 1 to 5">importance: 5</span></div><div class="task__content"><div class="task__formatted"><p>What is this code going to show?</p> <div id="rwaf0zoyt9" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( 1 &amp;&amp; null &amp;&amp; 2 );</code></pre> </div> </div> </div></div><button class="task__solution" type="button">solution</button><div class="task__answer"><div class="task__answer-content"><div class="formatted"><p>The answer: <code>null</code>, because it’s the first falsy value from the list.</p> <div id="czoxfpzx1i" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert(1 &amp;&amp; null &amp;&amp; 2);</code></pre> </div> </div> </div></div></div><button class="close-button task__answer-close" type="button" title="close"></button></div></div></div></div><div class="task tasks__task"><div class="task__header"><div class="task__title-wrap"><h3 class="task__title"><a class="main__anchor" href="#what-is-the-result-of-and-ed-alerts" name="what-is-the-result-of-and-ed-alerts">What is the result of AND'ed alerts?</a></h3><a class="task__open-link" href="/task/alert-and" target="_blank"></a></div><div class="task__header-note"><span class="task__importance" title="How important is the task, from 1 to 5">importance: 3</span></div><div class="task__content"><div class="task__formatted"><p>What will this code show?</p> <div id="g8fints5xy" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( alert(1) &amp;&amp; alert(2) );</code></pre> </div> </div> </div></div><button class="task__solution" type="button">solution</button><div class="task__answer"><div class="task__answer-content"><div class="formatted"><p>The answer: <code>1</code>, and then <code>undefined</code>.</p> <div id="z10cf594lu" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( alert(1) &amp;&amp; alert(2) );</code></pre> </div> </div> </div><p>The call to <code>alert</code> returns <code>undefined</code> (it just shows a message, so there’s no meaningful return).</p> <p>Because of that, <code>&amp;&amp;</code> evaluates the left operand (outputs <code>1</code>), and immediately stops, because <code>undefined</code> is a falsy value. And <code>&amp;&amp;</code> looks for a falsy value and returns it, so it’s done.</p> </div></div><button class="close-button task__answer-close" type="button" title="close"></button></div></div></div></div><div class="task tasks__task"><div class="task__header"><div class="task__title-wrap"><h3 class="task__title"><a class="main__anchor" href="#the-result-of-or-and-or" name="the-result-of-or-and-or">The result of OR AND OR</a></h3><a class="task__open-link" href="/task/alert-and-or" target="_blank"></a></div><div class="task__header-note"><span class="task__importance" title="How important is the task, from 1 to 5">importance: 5</span></div><div class="task__content"><div class="task__formatted"><p>What will the result be?</p> <div id="btfj8lv8k1" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( null || 2 &amp;&amp; 3 || 4 );</code></pre> </div> </div> </div></div><button class="task__solution" type="button">solution</button><div class="task__answer"><div class="task__answer-content"><div class="formatted"><p>The answer: <code>3</code>.</p> <div id="3nb0viiu6u" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>alert( null || 2 &amp;&amp; 3 || 4 );</code></pre> </div> </div> </div><p>The precedence of AND <code>&amp;&amp;</code> is higher than <code>||</code>, so it executes first.</p> <p>The result of <code>2 &amp;&amp; 3 = 3</code>, so the expression becomes:</p> <div id="utmaoh6983" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-none"><code>null || 3 || 4</code></pre> </div> </div> </div><p>Now the result is the first truthy value: <code>3</code>.</p> </div></div><button class="close-button task__answer-close" type="button" title="close"></button></div></div></div></div><div class="task tasks__task"><div class="task__header"><div class="task__title-wrap"><h3 class="task__title"><a class="main__anchor" href="#check-the-range-between" name="check-the-range-between">Check the range between</a></h3><a class="task__open-link" href="/task/check-if-in-range" target="_blank"></a></div><div class="task__header-note"><span class="task__importance" title="How important is the task, from 1 to 5">importance: 3</span></div><div class="task__content"><div class="task__formatted"><p>Write an <code>if</code> condition to check that <code>age</code> is between <code>14</code> and <code>90</code> inclusively.</p> <p>“Inclusively” means that <code>age</code> can reach the edges <code>14</code> or <code>90</code>.</p> </div><button class="task__solution" type="button">solution</button><div class="task__answer"><div class="task__answer-content"><div class="formatted"><div id="kc9bmm0oj1" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>if (age &gt;= 14 &amp;&amp; age &lt;= 90)</code></pre> </div> </div> </div></div></div><button class="close-button task__answer-close" type="button" title="close"></button></div></div></div></div><div class="task tasks__task"><div class="task__header"><div class="task__title-wrap"><h3 class="task__title"><a class="main__anchor" href="#check-the-range-outside" name="check-the-range-outside">Check the range outside</a></h3><a class="task__open-link" href="/task/check-if-out-range" target="_blank"></a></div><div class="task__header-note"><span class="task__importance" title="How important is the task, from 1 to 5">importance: 3</span></div><div class="task__content"><div class="task__formatted"><p>Write an <code>if</code> condition to check that <code>age</code> is NOT between <code>14</code> and <code>90</code> inclusively.</p> <p>Create two variants: the first one using NOT <code>!</code>, the second one – without it.</p> </div><button class="task__solution" type="button">solution</button><div class="task__answer"><div class="task__answer-content"><div class="formatted"><p>The first variant:</p> <div id="z2k7gm4ct8" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>if (!(age &gt;= 14 &amp;&amp; age &lt;= 90))</code></pre> </div> </div> </div><p>The second variant:</p> <div id="h9gl7zewb0" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>if (age &lt; 14 || age &gt; 90)</code></pre> </div> </div> </div></div></div><button class="close-button task__answer-close" type="button" title="close"></button></div></div></div></div><div class="task tasks__task"><div class="task__header"><div class="task__title-wrap"><h3 class="task__title"><a class="main__anchor" href="#a-question-about-if" name="a-question-about-if">A question about &quot;if&quot;</a></h3><a class="task__open-link" href="/task/if-question" target="_blank"></a></div><div class="task__header-note"><span class="task__importance" title="How important is the task, from 1 to 5">importance: 5</span></div><div class="task__content"><div class="task__formatted"><p>Which of these <code>alert</code>s are going to execute?</p> <p>What will the results of the expressions be inside <code>if(...)</code>?</p> <div id="qbqayvvs0i" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>if (-1 || 0) alert( 'first' ); if (-1 &amp;&amp; 0) alert( 'second' ); if (null || -1 &amp;&amp; 1) alert( 'third' );</code></pre> </div> </div> </div></div><button class="task__solution" type="button">solution</button><div class="task__answer"><div class="task__answer-content"><div class="formatted"><p>The answer: the first and the third will execute.</p> <p>Details:</p> <div id="1s1yl9gzxh" data-trusted="1" class="code-example"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>// Runs. // The result of -1 || 0 = -1, truthy if (-1 || 0) alert( 'first' ); // Doesn't run // -1 &amp;&amp; 0 = 0, falsy if (-1 &amp;&amp; 0) alert( 'second' ); // Executes // Operator &amp;&amp; has a higher precedence than || // so -1 &amp;&amp; 1 executes first, giving us the chain: // null || -1 &amp;&amp; 1 -&gt; null || 1 -&gt; 1 if (null || -1 &amp;&amp; 1) alert( 'third' );</code></pre> </div> </div> </div></div></div><button class="close-button task__answer-close" type="button" title="close"></button></div></div></div></div><div class="task tasks__task"><div class="task__header"><div class="task__title-wrap"><h3 class="task__title"><a class="main__anchor" href="#check-the-login" name="check-the-login">Check the login</a></h3><a class="task__open-link" href="/task/check-login" target="_blank"></a></div><div class="task__header-note"><span class="task__importance" title="How important is the task, from 1 to 5">importance: 3</span></div><div class="task__content"><div class="task__formatted"><p>Write the code which asks for a login with <code>prompt</code>.</p> <p>If the visitor enters <code>&quot;Admin&quot;</code>, then <code>prompt</code> for a password, if the input is an empty line or <kbd class="shortcut">Esc</kbd> – show “Canceled”, if it’s another string – then show “I don’t know you”.</p> <p>The password is checked as follows:</p> <ul> <li>If it equals “TheMaster”, then show “Welcome!”,</li> <li>Another string – show “Wrong password”,</li> <li>For an empty string or cancelled input, show “Canceled”</li> </ul> <p>The schema:</p> <figure><div class="image" style="width:599px"> <div class="image__ratio" style="padding-top:82.30383973288815%"></div> <object type="image/svg+xml" data="/task/check-login/ifelse_task.svg" width="599" height="493" class="image__image" data-use-theme> <img src="/task/check-login/ifelse_task.svg" alt="" width="599" height="493"> </object> </div></figure><p>Please use nested <code>if</code> blocks. Mind the overall readability of the code.</p> <p>Hint: passing an empty input to a prompt returns an empty string <code>''</code>. Pressing <kbd class="shortcut">ESC</kbd> during a prompt returns <code>null</code>.</p> <p><a href="#" onclick="event.preventDefault(); runDemo(this)">Run the demo</a></p></div><button class="task__solution" type="button">solution</button><div class="task__answer"><div class="task__answer-content"><div class="formatted"><div id="4v5zl1f1fv" data-trusted="1" class="code-example" data-demo="1"> <div class="codebox code-example__codebox"> <div class="toolbar codebox__toolbar"> <div class="toolbar__tool"> <a href="#" title="run" data-action="run" class="toolbar__button toolbar__button_run"></a> </div> <div class="toolbar__tool"> <a href="#" title="open in sandbox" target="_blank" data-action="edit" class="toolbar__button toolbar__button_edit"></a> </div> </div> <div class="codebox__code" data-code="1"> <pre class="line-numbers language-javascript"><code>let userName = prompt(&quot;Who's there?&quot;, ''); if (userName === 'Admin') { let pass = prompt('Password?', ''); if (pass === 'TheMaster') { alert( 'Welcome!' ); } else if (pass === '' || pass === null) { alert( 'Canceled' ); } else { alert( 'Wrong password' ); } } else if (userName === '' || userName === null) { alert( 'Canceled' ); } else { alert( &quot;I don't know you&quot; ); }</code></pre> </div> </div> </div><p>Note the vertical indents inside the <code>if</code> blocks. They are technically not required, but make the code more readable.</p> </div></div><button class="close-button task__answer-close" type="button" title="close"></button></div></div></div></div></div></div><div class="page__nav-wrap"><a class="page__nav page__nav_prev" href="/ifelse" data-tooltip="Conditional branching: if, '?'"><span class="page__nav-text"><span class="page__nav-text-shortcut"></span></span><span class="page__nav-text-alternate">Previous lesson</span></a><a class="page__nav page__nav_next" href="/nullish-coalescing-operator" data-tooltip="Nullish coalescing operator '??'"><span class="page__nav-text"><span class="page__nav-text-shortcut"></span></span><span class="page__nav-text-alternate">Next lesson</span></a></div><div class="article-tablet-foot tablet-only"><div class="article-tablet-foot__layout"><div class="share-icons"><span class="share-icons__title">Share</span><a class="share share_tw" href="https://twitter.com/share?url=https%3A%2F%2Fjavascript.info%2Flogical-operators" rel="nofollow"></a><a class="share share_fb" href="https://www.facebook.com/sharer/sharer.php?s=100&amp;p%5Burl%5D=https%3A%2F%2Fjavascript.info%2Flogical-operators" rel="nofollow"></a></div><div class="article-tablet-foot__map"><a class="map" href="/tutorial/map" data-action="tutorial-map"><span class="map__text">Tutorial map</span></a></div></div></div><div class="banner-bottom-carbon"><div id="javascriptinfo_iconbar"></div></div><script src="//m.servedby-buysellads.com/monetization.custom.js" id="javascriptinfo_script" async></script><script>document.getElementById("javascriptinfo_script").onload=function(){_bsa.init("custom","CKYDEK3U","placement:javascriptinfo_iconbar",{target:"#javascriptinfo_iconbar",template:'\n <div class="iconBarFlex">\n <a href="##statlink##" class="iconBarLink" rel="sponsored noopener" target="_blank" title="##company## — ##tagline##">\n <div class="iconBarImage" style="background-color: ##backgroundColor##;">\n <img height="30" width="30" src="##image##" alt="##company## logo">\n </div>\n <div class="iconBarText">\n <div class="iconBarTagline">##companyTagline##</div>\n <div class="iconBarDescription">##description##</div>\n </div>\n </a>\n <a href="##ad_via_link##" class="iconBarVia" rel="sponsored noopener" target="_blank">ads via BuySellAds</a>\n </div>\n '})};</script><div class="comments formatted" id="comments"><div class="comments__header"><h2 class="comments__header-title"><a href="#comments" name="comments">Comments</a></h2><div class="comments__read-before"><span class="comments__read-before-link">read this before commenting…</span><div class="comments__read-before-popup"><div class="comments__read-before-popup-i"><ul><li>If you have suggestions what to improve - please <a href="https://github.com/javascript-tutorial/en.javascript.info/issues/new">submit a GitHub issue</a> or a pull request instead of commenting.</li><li>If you can't understand something in the article – please elaborate.</li><li>To insert few words of code, use the <code>&lt;code&gt;</code> tag, for several lines – wrap them in <code>&lt;pre&gt;</code> tag, for more than 10 lines – use a sandbox (<a href='https://plnkr.co/edit/?p=preview'>plnkr</a>, <a href='https://jsbin.com'>jsbin</a>, <a href='http://codepen.io'>codepen</a>…)</li></ul></div></div></div></div><div id="disqus_thread"></div><script>var disqus_config = function() { if (!this.page) this.page = {}; Object.assign(this.page, {"url":"https:\/\/javascript.info\/logical-operators","identifier":"\/logical-operators"}); };</script><script>var disqus_shortname = "javascriptinfo";</script><script>var disqus_enabled = true;</script></div></script></main></div><div class="sidebar page__sidebar sidebar sidebar_sticky-footer"><button class="sidebar__toggle" data-sidebar-toggle></button><a class="map" href="/tutorial/map" data-action="tutorial-map" data-tooltip="Tutorial map"></a><div class="sidebar__inner"><div class="sidebar__content"><div class="sidebar__section"><h4 class="sidebar__section-title">Chapter</h4><nav class="sidebar__navigation"><ul class="sidebar__navigation-links"><li class="sidebar__navigation-link"><a class="sidebar__link" href="/first-steps">JavaScript Fundamentals</a></li></ul></nav></div><div class="sidebar__section"><h4 class="sidebar__section-title">Lesson navigation</h4><nav class="sidebar__navigation"><ul class="sidebar__navigation-links"><li class="sidebar__navigation-link"><a class="sidebar__link" href="#or">|| (OR)</a></li><li class="sidebar__navigation-link"><a class="sidebar__link" href="#or-finds-the-first-truthy-value">OR &quot;||&quot; finds the first truthy value</a></li><li class="sidebar__navigation-link"><a class="sidebar__link" href="#and">&amp;&amp; (AND)</a></li><li class="sidebar__navigation-link"><a class="sidebar__link" href="#and-finds-the-first-falsy-value">AND “&amp;&amp;” finds the first falsy value</a></li><li class="sidebar__navigation-link"><a class="sidebar__link" href="#not">! (NOT)</a></li></ul></nav></div><div class="sidebar__section"><nav class="sidebar__navigation"><ul class="sidebar__navigation-links"><li class="sidebar__navigation-link"><a class="sidebar__link" href="#tasks">Tasks (9)</a></li><li class="sidebar__navigation-link"><a class="sidebar__link" href="#comments">Comments</a></li></ul></nav></div><div class="sidebar__section"><div class="sidebar__section-title">Share</div><a class="share share_tw sidebar__share" href="https://twitter.com/share?url=https%3A%2F%2Fjavascript.info%2Flogical-operators" rel="nofollow"></a><a class="share share_fb sidebar__share" href="https://www.facebook.com/sharer/sharer.php?s=100&amp;p[url]=https%3A%2F%2Fjavascript.info%2Flogical-operators" rel="nofollow"></a></div><div class="sidebar__section"><a class="sidebar__link" href="https://github.com/javascript-tutorial/en.javascript.info/blob/master/1-js/02-first-steps/11-logical-operators" rel="nofollow">Edit on GitHub</a></div><div class="sidebar__section" id="sponsorBar"><div class="sidebar__section-title" id="sponsorBarTitle"></div><div id="sponsorBarContent"></div></div><script>window.initSponsorBar()</script></div></div></div></div></div><div class="page-footer"><ul class="page-footer__list"><li class="page-footer__item page-footer__item_copy">©&nbsp;2007—2025&nbsp; Ilya Kantor</li><li class="page-footer__item page-footer__item_about"><a class="page-footer__link" href="/about">about the project</a></li><li class="page-footer__item page-footer__item_contact"><a class="page-footer__link" href="/about#contact-us">contact us</a></li><li class="page-footer__item page-footer__item_terms"><a class="page-footer__link" href="/terms">terms of usage</a></li><li class="page-footer__item page-footer__item_privacy"><a class="page-footer__link" href="/privacy">privacy policy</a></li></ul></div></body></html>

Pages: 1 2 3 4 5 6 7 8 9 10