-1

Once and awhile I see and try to use an if statement like this:

var boo = true;

if(boo)
console.log("a");

as opposed to :

var boo = true;

if(boo == true){
 console.log("a");
}else{
 console.log("not true");
}

Is there a way to have an "else" with my first switch statement? How does the compiler even know when to stop? Does it do the conditional logic after a linebreak?

SD Dev
  • 335
  • 3
  • 10
  • [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy) and [if/else](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else) – epascarello Aug 12 '16 at 13:26

3 Answers3

8

An if is just followed by a statement. A block (which is typically and idiomatically used there) is just a type of statement that wraps up a group of other statements.

Is there a way to have an "else" with my first switch statement?

The same is true of else

if (1)
    something();
else
    something_else();

How does the compiler even know when to stop?

It allows a single statement.

Does it do the conditional logic after a linebreak?

No, a statement. The line break there just triggers ASI.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

The formal syntax for if else is something like:

if statement else statement

The generic "statement" there can be something like a simple assignment expression, a single function call, or a block of one or more statements enclosed in { }. Thus, the { } are not part of the syntactic structure of the if statement. They're part of the general and often-used compound statement. (JavaScript may formally call that something else; it's a common thing however and the name isn't so important. edit A comment notes that it's formally a "block statement", but I think "compound" is more descriptive so I'll leave it in.) A brace-enclosed compound statement contains any number of other statements, but from the outside it comprises just one statement.

The else part of the if statement is optional, so

if statement

is fine too.

Thus, in your examples:

if (boo)
  console.log(a);

The console.log(a); is the single statement. In the second example:

var boo = true;

if(boo == true){
 console.log("a");
}else{
 console.log("not true");
}

there is an else clause, and both the if and the else have compound statements playing the role of the single statement in the formal syntax.

As to exactly how the parser understands that, well, that's a big topic. Suffice to say that programming languages are designed to be as unambiguous as possible, so that the code to parse the raw text of a program can be as simple as possible. Of course, as with anything, no such design is perfect, and in particular JavaScript has syntax features that are surprisingly hard to parse. What we're talking about here, however, isn't one of those. The whole purpose of the { } wrapper around a compound statement is to give the parser a clear signal: here comes a compound statement.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block) calls it a block statement. Compound statement seems more descriptive though. – 4castle Aug 12 '16 at 13:34
  • @4castle thanks, I'll edit that in. I have a mental block against remembering appropriate terminology. – Pointy Aug 12 '16 at 13:35
-2

if (condition) stuff... else stuff...

if(true)
alert("A")
else
alert("B")

alternatively

if(false)
alert("A")
else
alert("B")
E.Serra
  • 1,495
  • 11
  • 14