Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7025331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:57:38+00:00 2026-05-27T23:57:38+00:00

I often see and use codes like: var myvar = (1 < 2) ?

  • 0

I often see and use codes like:

var myvar = (1 < 2) ? 3 : 4 ; //if 1 < 2 then myvar = 3, else = 4

But I just recently saw a code that was executing code, just like some kind of replacement for the if(){}else{}:

Example:

(1 < 2) ? alert("example1") : alert("example2");

The first thoughts that came to me were, “wow, this is like 6-7 characters shorter”, “endless of possibilities” or “this made my day”.

My question:

  • Is this thing error-free and safe to use? (like, with a lot of code inside, and nested stuff)

For now, I will just keep using it in the normal way, I have the fear that if I start using it to execute pieces of code might not work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:57:38+00:00Added an answer on May 27, 2026 at 11:57 pm

    There are some exceptions. You can’t do this with:

    • break
    • continue
    • Any block like if, for, while, do, or try

    for example. What’s more, it can mess with your order of operations:

    x < 3 ? l = true : r = true; // Syntax error, = has lower precedence than ?:
    

    But that’s not the reason not to do it, it’s because it’s ugly. Which one is clearer to you, this:

    if(i > 5) {
        alert('One');
    } else {
        alert('Two');
    }
    

    or

    i > 5 ? alert('One') : alert('Two');
    

    ? It’s not quite right, is it? And saving characters is never a reason to do anything, really; otherwise there would be no comments or whitespace. A good minifier like Google Closure Compiler will automatically convert these for you when possible, and there are plenty of other places to save. In the end, it’s just whatever you find most convenient and readable.

    Also, if you do end up needing break, continue, etc. then it’s going to be rather inconsistent and unattractive code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I often see code like: Iterator i = list.iterator(); while(i.hasNext()) { ... } but
I often see code like that which is shown here , ie where an
often in code that uses permissions checking, i see some folks use hex 0x0001
I use MVVM architecture to decouple my application. That is, you often see something
I see this often in the build scripts of projects that use autotools (autoconf,
While debugging jQuery apps that use AJAX, I often have the need to see
You often see, on sites like The Daily WTF , examples of overengineered code
I often see something like that: something.property|escape something is an object, property is it's
I often see code like this: // Approach 1 if(data != nil){ // Do
I often find I want to write code something like this in C#, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.