---
title: "Conditional operator in JavaScript"  
description: "Conditional operator in JavaScript"  
author: "Anonymous User"  
published: 2021-07-19  
updated: 2021-07-19  
canonical: https://www.mindstick.com/forum/156516/conditional-operator-in-javascript  
category: "javascript"  
tags: ["javascript"]  
reading_time: 1 minute  

---

# Conditional operator in JavaScript

How do you use the ? : (conditional) [operator](https://www.mindstick.com/blog/144/union-intersection-and-except-operator-in-sql-server) in [JavaScript](https://www.mindstick.com/articles/874/how-to-create-watermark-text-for-textbox-by-using-javascript)? And where to use this operator?

## Replies

### Reply by Ethan Karla

Basically for one word if else condition we use ternary operator. This is a one-line shorthand for an if-else statement and It's also known as the conditional operator.

Here is an example of code that could be shortened with the conditional operator:

```
var userType;
if (userIsYoungerThan18) {
  userType = 'Minor';
} else {
  userType = 'Adult';
}
```

This can be shortened with the ?: like so:

```
var userType = userIsYoungerThan18 ? 'Minor' : 'Adult';
```

Hope this information has cleared your confusion.

Happy Coding!


---

Original Source: https://www.mindstick.com/forum/156516/conditional-operator-in-javascript

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
