Meta tags:
Headings (most frequently used words):
javascript, the, for, web, conditional, operator, handling, objects, ternary, null, chaining, summary, chapter, article, toc, about, getting, started, basics, control, structures, data, types, built, in, operators, functions, classes, error, regular, expressions, general, bom, dom,
Text of the page (most frequently used words):
the (57), #operator (23), alert (20), #conditional (16), this (15), you (15), age (15), javascript (14), and (14), for (13), operators (12), else (11), introduction (11), statement (8), true (8), code (7), like (7), name1 (7), null (7), condition (7), your (6), ternary (6), article (5), not (5), statements (5), chaining (5), will (5), using (5), action (5), with (5), into (4), some (4), people (4), want (4), var (4), what (4), line (4), but (4), can (4), one (4), unknown (4), person (4), hello (4), bit (4), name2 (4), take (4), methods (4), functions (4), tutorial (3), language (3), nullish (3), coalescing (3), remainder (3), allows (3), are (3), that (3), make (3), short (3), use (3), them (3), prompt (3), infant (3), child (3), teenager (3), adult (3), operation (3), while (3), single (3), how (3), let (3), example (3), variable (3), well (3), value (3), then (3), used (3), function (3), where (3), also (3), handling (3), control (3), web (3), object (3), regular (3), expressions (3), catching (3), languages (2), here (2), previous (2), shorter (2), form (2), sometimes (2), referred (2), they (2), readable (2), others (2), concise (2), summary (2), notice (2), together (2), all (2), lines (2), more (2), instance (2), actually (2), quite (2), there (2), turn (2), actions (2), many (2), multiple (2), persontogreet (2), verbose (2), before (2), variables (2), supply (2), fallback (2), because (2), return (2), string (2), very (2), need (2), check (2), may (2), read (2), other (2), confirm (2), knew (2), was (2), guess (2), just (2), programming (2), answer (2), based (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), values (2), parameters (2), optional (2), logical (2), modify (2), assign (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, has, been, fully, translated, following, preferred, list, help, translate, click, czech, next, syntactic, sugar, hated, who, thinks, less, loved, being, each, second, option, introduce, new, again, consider, completely, unreadable, love, efficiency, reducing, those, course, free, add, linebreaks, whatever, way, feel, would, rewrite, really, see, flexible, doesn, have, stop, times, basically, creating, series, demonstrated, chain, illustrate, could, store, outcome, both, evaluates, third, part, works, result, which, ever, takes, case, either, name, returned, immediately, john, doe, situation, commonly, when, ability, especially, useful, specify, evaluate, false, directly, within, another, version, find, harder, likes, keeps, their, three, parts, from, first, albeit, long, actioniftrue, actioniffalse, easy, understand, fortunately, comes, notation, looks, wrote, construct, define, two, only, examples, spent, time, writing, powerful, structure, found, pretty, much, any, allowing, flow, several, conditions, wpf, php5, jquery, html5, css3, webforms, mvc, location, history, script, tag, general, replace, capturing, groups, match, regex, flags, options, throwing, finally, static, classes, extension, property, accessors, properties, recursive, callback, anonymous, default, json, math, date, number, built, type, conversion, undefined, strings, numbers, booleans, data, switch, structures, truthy, falsy, basic, syntax, world, tools, getting, started, libraries, webbrowser, about, chapter, table, contents, complete,
Text of the page (random words):
conditional ternary operator 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 remainder operator conditional ternary operator handling null s conditional operator chaining summary 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 conditional ternary operator in a previous article we spent a bit of time writing if statements a very powerful control structure found in pretty much any programming language allowing you to control the flow of your code based on one or several conditions we also wrote code using the if else construct where we define two actions but only take one of them based on the condition here s one of the examples we used var answer confirm is it true if answer true alert i knew it was true else alert oh well guess not then it s quite easy to read and understand but also a bit verbose fortunately for us javascript just like many other programming languages comes with a shorter notation for the if else statement in the form of the conditional operator sometimes referred to as the ternary operator it looks like this condition actioniftrue actioniffalse so just like the if else statement there are three parts a condition an action to take if the condition is true and an action to take if the action is not true this allows us to take all the 5 lines of code from the first example and turn it into a single albeit a bit long line of code confirm is it true alert i knew it was true alert oh well guess not then you may or may not like this short version of the if else statement some people find it harder to read while other people likes how it keeps their code short and concise handling null s a situation where this operator is very commonly used is when you need to check a value for null before using it with the ability to supply a fallback value this is especially useful in javascript where you can specify a variable as the condition because it will evaluate to false if the variable is null this also allows us to do the check directly within another statement for instance like we do in this example let name1 john doe name2 null alert hello name1 name1 unknown person alert hello name2 name2 unknown person notice how i use the name1 and name2 variables as both the condition as well as the action if the condition evaluates to true as the third part of the statement i supply a fallback value this works because the conditional operator will actually return the result of which ever action it takes so in this case either the name or the string unknown person will be returned and then immediately used by the alert function to illustrate that we could make the example a bit more verbose and store the outcome of the conditional operation in a variable before using it let name1 null let persontogreet name1 name1 unknown person alert hello persontogreet conditional operator chaining as you can see the conditional operator is quite flexible but it doesn t have to stop there you can turn of the actions into a conditional operation as many times as you want basically creating a series of if else statements in one line in the article on if statements i demonstrated how to chain multiple if else statements together like this var age prompt what s your age if age 1 alert infant else if age 13 alert child else if age 18 alert teenager else alert adult but you can actually rewrite this as a single line if you really want to using the conditional operator var age prompt what s your age age 1 alert infant age 13 alert child age 18 alert teenager alert adult notice that for each conditional statement i use the second option to introduce a new conditional operation chaining them together again some people will consider this completely unreadable while others will love the efficiency of reducing all those lines of code to a single line you are of course free to add some linebreaks if you want to in whatever way you feel would make it more readable for instance like this var age prompt what s your age age 1 alert infant age 13 alert child age 18 alert teenager alert adult summary the conditional ternary operator allows you to do if else statements in a shorter form sometimes referred to as syntactic sugar they are hated by some people who thinks that they make the code less readable and loved by others for being short and concise use them if you want to remainder operator previous nullish coalescing 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
|