Meta tags:
Headings (most frequently used words):
the, logical, operator, javascript, operators, for, web, or, and, not, objects, summary, chapter, article, toc, about, getting, started, basics, control, structures, data, types, built, in, functions, classes, error, handling, regular, expressions, general, bom, dom, combining, double,
Text of the page (most frequently used words):
the (93), #operator (46), and (41), operators (28), logical (25), true (24), you (23), this (20), not (20), false (20), #javascript (19), alert (16), that (14), let (12), example (12), statement (12), introduction (11), are (10), can (10), for (10), conditions (10), into (9), value (9), have (9), which (8), boolean (8), condition (8), will (8), here (7), use (7), but (7), about (6), one (6), falsy (6), like (6), else (6), than (6), two (6), with (6), article (5), programming (5), now (5), because (5), where (5), parentheses (5), soon (5), languages (4), previous (4), need (4), some (4), nullish (4), coalescing (4), want (4), double (4), considered (4), actual (4), right (4), when (4), using (4), from (4), may (4), notice (4), conditional (4), well (4), many (4), get (4), require (4), fails (4), function (4), confirm (4), methods (4), functions (4), tutorial (3), has (3), remainder (3), modify (3), assign (3), all (3), basically (3), turning (3), result (3), while (3), values (3), used (3), message (3), variable (3), was (3), also (3), written (3), other (3), added (3), behavior (3), bigger (3), sure (3), what (3), entire (3), course (3), return (3), they (3), since (3), variables (3), more (3), just (3), web (3), object (3), regular (3), expressions (3), catching (3), your (2), language (2), next (2), logic (2), main (2), discuss (2), last (2), summary (2), then (2), conversion (2), number (2), something (2), negated (2), illustrated (2), above (2), time (2), works (2), take (2), truthy (2), even (2), though (2), initially (2), make (2), basic (2), would (2), looking (2), either (2), precedence (2), uses (2), combine (2), aware (2), called (2), order (2), evaluated (2), situations (2), group (2), accordingly (2), combining (2), call (2), greater (2), again (2), each (2), left (2), met (2), both (2), should (2), whenever (2), remaining (2), checked (2), three (2), general (2), asp (2), net (2), dom (2), window (2), bom (2), search (2), error (2), types (2), errors (2), try (2), catch (2), fields (2), inheritance (2), getters (2), setters (2), prototypes (2), objects (2), parameters (2), optional (2), chaining (2), ternary (2), increment (2), decrement (2), arithmetic (2), comparison (2), assignment (2), array (2), basics (2), loops (2), toc (2), localization, suggest, correction, contact, com, 2007, 2026, been, fully, translated, following, preferred, list, help, translate, click, czech, obviously, talked, articles, see, negate, under, circumstances, described, converted, becomes, run, mainly, sometimes, turn, succeed, displayed, before, checking, yes, second, line, very, action, tell, commonly, referred, negation, exclamation, point, expression, operand, reverse, situation, add, instruct, parts, grouped, rewritten, nothing, changed, besides, yet, returning, instead, goal, create, first, skips, secondary, jumps, third, wanted, causing, evaluate, free, single, mixing, decide, statements, lead, unexpected, these, possible, doing, within, set, depending, saw, reached, case, reach, middle, part, zero, failed, illustrate, once, thereby, its, entirety, test, changing, ampersands, hit, skip, matter, how, look, evaluates, relevant, cases, there, heavy, processing, those, verify, show, won, our, write, them, noted, vertical, pipes, fourth, talk, separately, later, find, primary, wpf, php5, jquery, html5, css3, webforms, mvc, location, history, script, tag, replace, capturing, groups, match, regex, flags, options, throwing, finally, handling, static, classes, extension, property, accessors, properties, multiple, recursive, callback, anonymous, default, json, math, date, string, built, type, null, undefined, strings, numbers, booleans, data, switch, control, structures, syntax, hello, world, tools, getting, started, libraries, webbrowser, chapter, table, contents, complete,
Text of the page (random words):
logical operators the complete javascript tutorial javascript tutorial toc table of contents chapter article toc introduction the assignment operator comparison operators arithmetic operators increment decrement operators modify and assign operators logical operators the logical or operator the logical and operator combining or and operators the logical not operator the double not operator summary remainder operator conditional ternary operator nullish coalescing operator optional chaining operator about javascript what is javascript javascript and the webbrowser javascript libraries getting started the tools you need hello world the basics basic javascript syntax variables truthy falsy control structures the if statement the switch statement loops for and while loops for of and for in data types introduction booleans numbers strings null undefined type conversion built in objects introduction number string array the basics array methods date math json operators introduction the assignment operator comparison operators arithmetic operators increment decrement operators modify and assign operators logical operators remainder operator conditional ternary operator nullish coalescing operator optional chaining operator functions introduction function parameters function parameters with default values anonymous functions callback functions recursive functions multiple return values objects introduction properties methods property accessors getters setters prototypes prototypes inheritance extension classes introduction fields methods getters setters inheritance static fields methods error handling introduction catching errors try catch catching throwing errors try catch finally error types conditional catching regular expressions introduction regex flags options search match with regular expressions using capturing groups search replace with regular expressions javascript for web general introduction window dom bom the script tag javascript for web the bom the window object the history object the location object javascript for web the dom introduction javascript asp net mvc asp net webforms c css3 html5 jquery php5 wpf operators logical operators programming in general is all about logic so of course we need some logical operators as well in javascript you will find three primary logical operators the or operator the and operator the not operator a fourth operator called the nullish coalescing operator written as is also considered a logical operator but we ll talk about that one separately in a later article for now let s discuss the three main logical operators the logical or operator the logical or operator in javascript as well as in many other programming languages is noted as two vertical pipes like this you can use it whenever you want to write a statement with two or more conditions where just one of them have to be true here s an example let a 2 b 0 if a 0 b 0 alert true else alert false in this example i just require that one of our variables are greater than 0 and since the a variable is we get the true message be aware that each condition is checked from left to right but as soon as an or operator is met and a previous condition evaluates to true the remaining conditions won t be checked this can be relevant in cases where there are many conditions and or they require heavy processing in those situations you may want to order the conditions accordingly let s verify this behavior while i also show you that we can of course have more than two conditions let a 42 b 0 if a 0 b 0 confirm are you sure alert true else alert false you will notice that even though i added a call to the confirm function the example works just like the previous one because as soon as a condition is true and we hit an or operator we skip the remaining conditions no matter how they look the logical and operator the logical and operator should be written as two ampersands in javascript and many other programming languages like this it should be used whenever you have two or more conditions which all have to be true for the statement to be true in its entirety we can test this by using the example above but changing the operator from or to and like this let a 2 b 0 if a 0 b 0 alert true else alert false since we now use the and operator both conditions have to be true we thereby require that both variables are bigger than 0 and since the b variable is not the statement fails and we get the false message and again you will notice that each conditions is evaluated from left to right and as soon as the and operator is met and the previous condition failed the entire statement fails we can illustrate this once again with an example let a 42 b 0 if a 0 b 0 confirm are you sure alert true else alert false as we saw with the or example the call to the confirm function is not reached in this case because the entire statement fails as soon as we reach the middle part where b has to be greater than zero combining or and operators in some situations you may need to combine these two operators and or of course this is possible but you may have to use parentheses to group the operators accordingly to get the result you re looking for when doing so the conditions within a set of parentheses are considered a group and will return either true or false depending on which operators you use and what they return you are free to combine logical operators into a single statement but when mixing logical operators you have to be aware of something called operator precedence javascript uses this to decide the order in which the statements are evaluated which can lead to some unexpected behavior as illustrated by this example let a 42 b 0 c 20 if a 0 b 0 c 0 alert true else alert false my goal here was to create a statement that would require that a is 0 and that either b or c is bigger than 0 but because of operator precedence as soon as the first condition fails because a is not 0 it skips the secondary condition and jumps to the third condition which uses the or operator to make sure that c is bigger than 0 which it is causing the entire statement to evaluate to true this is not what we wanted in a situation like this we have to add parentheses to instruct javascript which parts of the statement we want grouped here s a rewritten example where i have added parentheses you will notice that nothing has changed besides the added parentheses and yet now we get the behavior i was initially looking for returning false instead of true let a 42 b 0 c 20 if a 0 b 0 c 0 alert true else alert false the logical not operator the last logical operator i would like to tell you about is the logical not operator it s also commonly referred to as the negation operator and it s written as an exclamation point in javascript as well as in many other programming languages when using it it will take the expression operand and reverse it basically turning true to false and false to true here s a very basic example of it in action let a false a a alert a true notice the second line where i use the logical not operator to make a false boolean value into a true value this can be used in a conditional statement as well let a false if a alert yes in this example the condition will succeed and the message will be displayed even though the a variable was initially false because we use the not operator right before checking it basically turning the false value into true the double not operator from time to time you may run into the double not operator while the logical not operator mainly works on actual boolean values it is sometimes used to take a value that is truthy or falsy and turn it into an actual boolean value here s an example let a 0 a a alert a true the number 0 is considered a falsy value not an actual boolean false but something that can be considered false under the right circumstances as described here when using the not operator on a falsy value it s converted into an actual boolean and then negated so a falsy value negated by the logical not operator becomes a boolean true and so on as illustrated by the example above now if you want to negate the result of this conversion result you can use the double not operator let a 0 a a alert a false as you can see we are now basically turning a falsy value into boolean true and then to a boolean false summary programming is all about logic so obviously we need some logical operators we talked about the or and and not operators in this article which are the main logical operators of javascript in one of the next articles we ll discuss the last one the nullish coalescing operator modify and assign operators previous remainder operator next this article has been fully translated into the following languages czech is your preferred language not on the list click here to help us translate this article into your language javascript tutorial com 2007 2026 contact us suggest correction localization
|