Piotr Sikora - Logo - Automatyzacja procesów | AI | JavaScript | Front End | Team Leader
  • Home
  • Services
    • Process Automation
    • AI for Lawyers
  • Blog
  • Quotes
  • Contact
PL/EN

Conditional (Ternary) Operator in JavaScript Guide

  • Home
  • Blog
  • Conditional (Ternary) Operator in JavaScript Guide
Conditional (Ternary) Operator in JavaScript Guide

By Piotr Sikora

  • development

  • 2 min read

Let's imagine that we could optimize / minify if statement. What could we do?

In case we have if...else statement like:

if (condition) {
// do sth if condition is true
}
else {
// do sth else if condition isn't true
}

we can change it to ternary expression

condition ? /* do sth if condition is true */ : /* do sth if condition isn't true */;

Let's check how it works with functions:

const actionOne = () => console.log('actionOne');
const actionTwo = () => console.log('actionTwo');

true ? actionOne() : actionTwo(); // actionOne
false ? actionOne() : actionTwo(); // actionTwo

And how to use it with more complex functions:

const multiply = (a) => a*a;
const add = (a) => a+a;

const variable = 4;

let newVar;

newVar = true ? multiply(variable) : add(variable);

console.log(newVar) // 16

newVar = false ? multiply(variable) : add(variable);

console.log(newVar) // 8

As you can see ternary can be useful when you want to make inline if operations.

Share this article

TwitterLinkedInFacebook

Tags:

  • #javascript

  • #js

  • #nodejs

  • #typescript

Categories

after-hours(1)AI(7)ai-en(1)angular(4)automatic-tests(1)Automation(2)cryptography(1)css(8)CyberSecurity(2)Development(6)DevOps(1)events(3)javascript(11)n8n(10)ollama(1)security(2)seo(1)

Recent Posts

Testing Kimi Code: First Impressions from Web and CLI

Development

Testing Kimi Code: First Impressions from Web and CLI

Why You Shouldn't Cram Multiple Webhooks Into One n8n Workflow

Automation

Why You Shouldn't Cram Multiple Webhooks Into One n8n Workflow

DRY, WET, AHA: Finding the Right Balance in Code Reuse

Development

DRY, WET, AHA: Finding the Right Balance in Code Reuse

API vs Webhook: Understanding the Difference

Development

API vs Webhook: Understanding the Difference

RTCROS Framework: Structure Your Prompts for Better AI Results

AI

RTCROS Framework: Structure Your Prompts for Better AI Results

About Me

Piotr Sikora - Process Automation | AI | n8n | Python | JavaScript

Piotr Sikora

Process Automation Specialist

I implement automation that saves time and money, streamlines operations, and increases the predictability of results. Specializing in process automation, AI implementation, and workflow optimization using n8n, Python, and JavaScript.

n8n Workflows

n8n workflow automation templates

Explore my workflow templates on n8n. Ready-to-use automations for blog management, data collection, and AI-powered content processing.

3Workflow Templates
View Templates

• Auto-Categorize Blog Posts with AI

• Collect LinkedIn Profiles

• Export WordPress Posts for SEO

Tags

activepiecesafter-hoursahrefsaiAI cost reductionai-agentsai-automationangularantigravityapiAqua Securityarcade-gamesarchitectureautomationbackendbest-practicescadillacs-and-dinosaurschatbotchatgptCI/CD
“Whether you think you can or you think you can't – you're right.”
Henry Ford
View more quotes

Similar Articles

Discover more related content

Unleash the Power of Autoexecuted Objects in JavaScript

Unleash the Power of Autoexecuted Objects in JavaScript

Sometimes you will need to create autoexecuted object (for example in case of load/document ready events)

31 January 2014
Pure JavaScript: Private and Public Methods Guide

Pure JavaScript: Private and Public Methods Guide

Have you been creating your own classes in pure JavaScript?

Raspberry Pi node.js and how to start with programming GPIO

Raspberry Pi node.js and how to start with programming GPIO

Start programming Raspberry Pi GPIO with Node.js. Introduction to the PixPress project on GitHub. Hardware meets JavaScript.

Piotr Sikora | ai | n8n | javascript | python

Let's get to know each other!

Get in touch with me
Piotr Sikora | Process Automation  | AI Implementation 🤖 | Technology Consulting
Quick links
  • Home
  • Blog
  • Contact
Contact
  • piotr.sikora.ck@gmail.com
  • +48 505 684 661

© Piotr Sikora 2026 | All Rights Reserved