DEV Community

Cover image for Javascript Ternary Operator
Tobi Ajibade
Tobi Ajibade

Posted on

2 1

Javascript Ternary Operator

Conditional or ternary Operator

A conditional operator assigns a specified value to a variable based on some conditions, i.e it assigns a value to a variable if a condition is true , otherwise assigns an alternate value if condition is false.

Syntax

variable name = (condition) ? value1 : value2;

Practical Example

Let write a script to check how experience a web developer is, depending on their years of experience in the industry

Javascript code


function checkExp() {
var input, output;
input = document.getElementById("expcheck").value;
  output = (input < 2) ? "You're a young developer" : "You're an experienced developer";
document.getElementById("output").innerHTML = output;
}

Enter fullscreen mode Exit fullscreen mode

Below is the snippet in codepen

This is actually my first post. Thanks for reading!

Free to hit me up in the comment section.

[deleted user] image

[Deleted User]

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay