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 6243147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:05:07+00:00 2026-05-24T12:05:07+00:00

In Javascript, if an if expression does not have a curly bracket after it,

  • 0

In Javascript, if an if expression does not have a curly bracket after it, the following statement is put inside the if block. That is,

if(foo)
bar();
baz();

is equivalent to

if(foo) {
  bar();
}
baz();

Douglas Crockford recommends not using the first because it is confusing and can cause hard-to-trace bugs if a programmer tries to add a statement to an if block without braces. For this reason, JsLint complains if you use the first form.

I use this all the time, and I feel like it’s a non-issue provided that you put the statement on the same line as the if statement, like this:

if(foo) bar();
baz();

This is more concise visually than the full bracket form, and I’ve never had confusion with it. Just so I could pass JsLint and not have so much visual noise I have sometimes resorted to using a less idiomatic form that relies on operator short-circuiting, like this:

foo && bar();
baz();

You’re probably all waiting for me to hurry up and ask a question, so here it goes:
Is it generally considered bad practice to not use braces on one-line conditional statements if you format it correctly? Why? Is there a legitimate reason for JsLint to complain about it?

  • 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-24T12:05:08+00:00Added an answer on May 24, 2026 at 12:05 pm

    In reading:

    • You might not be used to that coding style, and might think that this executes the two functions:

      if(foo) bar();
              baz();
      
    • Since JS has automatic semicolon insertion, there are even murkier circumstances. I, for instance, am not comfortable with what ASI would do to this code, even if I can reason it out:

      if(foo) bar()
              baz()
      

      It is not impossible to figure out or even hard if you know what ASI does, but ASI is a nontrivial algorithm. Having to run it in your head is expensive in terms of time, so it’s better if you simply avoid ambiguous (in this sense, not ambiguous as per the ECMA-262 spec) situations.

    • It makes your lines longer than they need to be. This may be considered a non-issue depending on your coding conventions, if your condition is long, and your statement is as well, the resulting single-line statement will be more taxing on the eyes to read (it is well known that your eyes have difficulty reading very lone lines, hence paragraphing).

      You can, if you for some reason despite braces, still use something of the form:

      if(foo)
          bar();
      

      And that is virtually identical to the code you had, but I see no reason, at that point, not to just put the braces there.

    In writing:

    • When you need to add another statement, it is not the case that you’ll simply write it. You will have to refactor the surrounding area in order to do this. Again, this is not hard at all, but it is something extra to think about, which is not related to neither your problem nor your solution of the problem, it is merely a syntactical quirk.

      I think you already get the point, but it would be this scenario.

      Initial code:

      if(foo) bar();
      

      Changed code:

      if(foo) bar(); baz();
      

      You can see the problem clearly, but it might not necessarily pop up in a routine code review. If your testcases don’t cover this specific codepath, this might get pushed to production because of repeated coder oversights, which are made easier by not requiring blocks be delimited explicitly.
      The solution you’d could adopt, as you have, is to say something like if(foo) bar() && baz(), but that would fail if bar() is falsy, so you end up with ugly stuff like if(foo) (bar(), baz()); which works, but is decidedly very ugly.


    Personally, I use single-line if statements solely when the line is very short, and the algorithm itself is short as well. Something like if(extra_loop) --i; or if(!valid) break; When you start adding elses to these single-line ifs, the structure becomes more and more dangerous to manipulate.

    In short, you can use it, but use it knowing the pros and cons, just like any other tool.

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

Sidebar

Related Questions

I have the following very simple Javascript-compatible regular expression: <script type=text/javascript id=(.+) src=([^]+)> I
I am try to write a regular expression in javascript that will not allow
I have the same expression in Javascript but it won't work in PHP for
When a regular expression is run JavaScript, is the regex engine that evaluates the
The following javascript code concatenates the matches found by a regular expression. The regular
I need to write a regular expression that finds javascript files that match <anypath><slash>js<slash><anything>.js
Is there a way to have Expression Language (EL) expressions included JavaScript files be
I am trying to build a regular expression in javascript that checks for 3
I have a very basic regular expression that I just can't figure out why
I have a generic JavaScript function which takes one parameter function foo(val) { ...}

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.