You have several options:

  1. An if statement

    if (ve.select) {
        price += ve.price;
    }
    

    Or if you prefer it on one line:

    if (ve.select) price += ve.price;
    
  2. The conditional operator version you've shown:

    price = ve.select ? price + ve.price : price;
    

    ...but note that it does an unnecessary assignment when ve.select is falsy (though as price is presumably a simple variable that's not an issue; with an object property that might be an accessor property it can matter).

  3. Using the && (logical AND) operator:

    ve.select && price += ve.price;
    

    But note that this is really just an if statement in disguise.

  4. Tamás Sallai's answer providing an alternative use of the conditional operator (price += ve.select ? ve.price : 0;).

Answer from T.J. Crowder on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Conditional_operator
Conditional (ternary) operator - JavaScript | MDN
The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark (?), then an expression to execute if the condition is truthy followed by a colon (:), and finally the expression to execute if the condition is falsy.
Discussions

Question on if/when to use Ternary Operators?
Use “if” for statements and use ternary for expressions. For example, the appropriate way to use the ternary would go like this: console.log(nightTime ? "Nighttime" : "Daytime") Notice that this ternary is an expression that evaluates to a value. More on reddit.com
🌐 r/learnjavascript
17
5
December 29, 2024
Javascript ternary operator syntax with object
The ternary operator needs to have an else clause that tells it what to do if the condition is false. More on reddit.com
🌐 r/learnprogramming
10
4
September 9, 2023
🌐
W3Schools
w3schools.com › js › js_if_ternary.asp
W3Schools.com
The conditional operator is a shorthand for writing conditional if...else statements. It is called a ternary operator because it takes three operands.
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › conditional-types.html
TypeScript: Documentation - Conditional Types
We just found ourselves using conditional types to apply constraints and then extract out types. This ends up being such a common operation that conditional types make it easier.
🌐
freeCodeCamp
freecodecamp.org › news › the-ternary-operator-in-javascript
JavaScript Ternary Operator – Syntax and Example Use Case
January 6, 2023 - You cannot use the operator without assigning the returned value to a variable: const result = condition ? trueExpression : falseExpression · The returned value depends on the evaluation of the condition expression. If the condition is true, the returned value returned from trueExpression is assigned to the variable. Else, the returned value from falseExpression will be assigned to the variable. The ternary operator can be a good replacement for if statements in some cases.
Find elsewhere
🌐
Programiz
programiz.com › typescript › ternary-operator
TypeScript Ternary Operator
In TypeScript, both the ternary operator and the if...else statement serve similar purposes: to execute code conditionally based on certain conditions.
🌐
Reddit
reddit.com › r/learnjavascript › question on if/when to use ternary operators?
r/learnjavascript on Reddit: Question on if/when to use Ternary Operators?
December 29, 2024 -

So i am completly new to programming and learning thru codecademy.

I just got thru all the lessons about if else statements and when/how to use them but in their next lession they talk about Ternary Operator basically being a "shot handed" version of wirting an if else statement (if I am understanding that correctly) if I am understanding it correctly then my question is, is one more "professional" then the other or is it just based on what your coding or what lets say your boss is asking you to code

The other reason I ask is I want to devlope good habits now vs later down the road so using the example below is it I guess from a "real world" working senario is it better to use one over the other

For example; I know this is a very genaric and basic example but

let nightTime = true

if (nighTime) {
    console.log('Nightime');
} else {
    console.log('Daytime')
}

vs

nightTime
    ? console.log('Nighttime')
    : console.log('Daytime');
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-ternary-operator
JavaScript Ternary Operator - GeeksforGeeks
The Ternary Operator in JavaScript is a conditional operator that evaluates a condition and returns one of two values based on whether the condition is true or false.
Published   January 15, 2026
🌐
freeCodeCamp
freecodecamp.org › news › why-a-ternary-operator-is-not-a-conditional-operator-in-js
Why a Ternary Operator is not a Conditional Operator in JS
January 17, 2023 - The reason why many people (including myself, until recently) call the ternary operator a conditional operator, is because the conditional operator is the only ternary operator in JavaScript (and some other languages as well).
🌐
Medium
medium.com › @enayetflweb › understanding-typescript-operators-ternary-nullish-coalescing-and-optional-chaining-a69b8d9bbb35
Understanding TypeScript Operators: Ternary, Nullish Coalescing, and Optional Chaining | by Md Enayetur Rahman | Medium
May 5, 2024 - The ternary operator helps make concise decisions, the nullish coalescing operator provides default values for null or undefined variables, and optional chaining ensures safe property access within nested objects.
🌐
Medium
sushantsy.medium.com › mastering-ternary-operators-in-javascript-a-comprehensive-guide-with-interactive-examples-5eab30fca1d0
Mastering Ternary Operators in JavaScript: A Comprehensive Guide with Interactive Examples | by Sushant Yadav | Medium
January 12, 2024 - Ternary operators are a valuable tool in JavaScript, providing a concise and expressive way to handle conditional statements. Mastering them will not only make your code more readable but also help you write more efficient and elegant solutions.
🌐
Tektutorialshub
tektutorialshub.com › home › typescript › ternary conditional operator typescript
Ternary Conditional Operator Typescript - Tektutorialshub
March 15, 2023 - The Typescript conditional operator is a Ternary Operator, which takes three operands. The first operand is a condition to evaluate. It is followed by a question mark (?), then an expression (expression1).
🌐
freeCodeCamp
freecodecamp.org › news › javascript-ternary-operator-explained
How to Use the Ternary Operator in JavaScript – Explained with Examples
February 27, 2024 - The truthyValue and falsyValue can be anything in JavaScript. It can encompass various entities such as functions, values stored in variables, objects, numbers, strings, and more. The ternary operator grants you the flexibility to return any desired value, offering versatility in your code.
🌐
W3Schools
w3schools.com › react › react_es6_ternary.asp
React ES6 Ternary Operator
The ternary operator is a simplified conditional operator like if / else.
🌐
SitePoint
sitepoint.com › javascript
Ternary operator
August 22, 2010 - It’s well know that the ternary operator sintax is something like so: (test) ? true doThis : false doThat Suppose that in case condition is false we want do nothing. How to code “do nothing”? May I let it blank or ther…
🌐
Reddit
reddit.com › r/learnprogramming › javascript ternary operator syntax with object
r/learnprogramming on Reddit: Javascript ternary operator syntax with object
September 9, 2023 -

Hello, i got some issues with the syntax for this line of code:

counter === null ? counter = {wins: 0, losses:0, ties:0};

It tells me there is some issue with the ';'. I never had such a problem with ternary operators before, it seems like to be related with the object inside of it. what would be the right declaration of it?

Thanks

🌐
Frontend Masters
frontendmasters.com › courses › intermediate-typescript-v2 › ternary-operators-expressing-conditions
Ternary Operators & Expressing Conditions - Intermediate TypeScript, v2 | Frontend Masters
Mike introduces conditional types as a way to express type information similar to a ternary operator. How to define a conditional type that evaluates to different types based on a condition, and …
🌐
W3Schools
w3schools.com › c › c_conditions_short_hand.php
C Short Hand If ... Else (Ternary Operator)
The ternary operator returns a value based on a condition: if the condition is true, it returns the first value; otherwise, it returns the second value.
🌐
johnnyreilly
johnnyreilly.com › home › blog › the ternary operator
The Ternary Operator <3 Destructuring | johnnyreilly
August 19, 2016 - This third point only applies if you're using TypeScript (and I am): I have to specify the types of my variables up front if I want type safety. ES2015 gives us another option. We can move back to the ternary operator if we change the return type of each branch to be an object sharing the same ...