The syntax would be:
var icon = (area == 1) ? icon1 : (area == 2) ? icon2 : icon0;
But this is starting to get complicated. You may well be better off just creating a function to do this work instead:
var icon = getIcon(area);
function getIcon(area) {
if (area == 1) {
return icon1;
} else if (area == 2) {
return icon2;
}
return icon0;
}
Related Contents:
- How do you use the ? : (conditional) operator in JavaScript?
- How to write an inline IF statement in JavaScript?
- Omitting the second expression when using the if-else shorthand
- Question mark and colon in JavaScript
- JavaScript shorthand ternary operator
- Operator precedence with JavaScript’s ternary operator
- How to write a ternary operator (aka if) expression without repeating yourself
- Javascript one line If…else…else if statement
- Alternative to nested ternary operator in JS [closed]
- JavaScript shorthand if statement, without the else portion
- Why would a JavaScript variable start with a dollar sign? [duplicate]
- React-router URLs don’t work when refreshing or writing manually
- JavaScript file upload size validation
- Retrieve data from a ReadableStream object?
- Do something if screen width is less than 960 px
- Detect Safari browser
- Are braces necessary in one-line statements in JavaScript?
- How to check if a string is a valid hex color representation?
- Get the device width in javascript
- Javascript roundoff number to nearest 0.5
- Copy / Put text on the clipboard with FireFox, Safari and Chrome
- How do I get first element rather than using [0] in jQuery?
- Removing object properties with Lodash
- javascript filter array of objects
- Parsing ISO 8601 date in Javascript
- How to trigger checkbox click event even if it’s checked through Javascript code?
- Confused about useBuiltIns option of @babel/preset-env (using Browserslist Integration)
- What do double brackets mean in javascript and how to access them
- Getting an absolute URL from a relative one. (IE6 issue)
- Firestore: Multiple conditional where clauses
- JQuery select2 set default value from an option in list?
- Persistent Service Worker in Chrome Extension
- TypeError: Cannot assign to read only property ‘0’ of object ‘[object Array]’ in typescript
- ng-mouseover and leave to toggle item using mouse in angularjs
- Where is the console API for WebKit/Safari?
- How to format/tidy/beautify in JavaScript
- Running JavaScript unit tests headlessly in a Continuous Integration build
- Calling a method from another method in the same class
- Trigger autocomplete without submitting a form
- XMLHttpRequest changes POST to OPTION
- Open a new tab with custom HTML instead of a URL
- Prevent anchor behaviour
- Failed to load resource: net::ERR_NETWORK_IO_SUSPENDED
- Javascript replace method, replace with “$1”
- React Native Invariant Violation: View config
- Node.JS: How to pass variables to asynchronous callbacks? [duplicate]
- Adding CSRFToken to Ajax request
- How to get function body text in JavaScript? [duplicate]
- Convert string to Pascal Case (aka UpperCamelCase) in Javascript
- Is there an event for when a Chrome Extension popup is closed?