1

I'm new to Javascript and coding, so this is likely basic knowledge, but this example works, despite having no semicolons in a function for the if/else statements, or even curly braces {}. I thought they were both needed if you had multiple conditions within an if/else. Two have single conditions, so I understand they wouldn't need both semicolon and curly brace, but this has neither. And the else statement has 2 conditions, still no semicolon or curly brace and yet the code still works. Thanks for any insight.

This other question (that was closed but answered - Why is it that semicolons are not used after if/else statements?) had notes addressing the use of ; or {} but did not address if the syntax requirement somehow changes for if/else within a function.

http://www.javascriptkit.com/howto/show2.shtml

<script type="text/javascript">

//variable that will increment through the images
var step=0

function slideit(){
 //if browser does not support the image object, exit.
 if (!document.images)
  return document.getElementById('slide').src = slideimages[step].src
 if (step<2)
  step++
 else
  step=0
 //call function "slideit()" every 2.5 seconds
 setTimeout("slideit()",2500)
}

slideit()

</script>
Community
  • 1
  • 1

2 Answers2

1

Braces are needed for multiple lines, semi colons are not needed after those ever -- the syntax is not different in a function :)

https://www.codecademy.com/forum_questions/507f6dd09266b70200000d7e

antman
  • 227
  • 2
  • 17
0

Semicolons come after if or else in JavaScript. They would belong after statements inside if or else blocks but they are technically optional.

if blocks start with if and contain a statement block which is either one expression or { + one or more expressions + }. else blocks work the same way.

Thus, all of these are equivelent:

if console.log("foo")

if console.log("foo");

if
    console.log("foo")

if
    console.log;

if { console.log("foo") }

if { console.log("foo"); }

if {
    console.log("foo")
}

if {
    console.log("foo");
}

else blocks work the same way.


For what it is worth, you should always include semicolons in JavaScript (where appropriate). They are optional but every JavaScript code guideline I've ever seen tells you to use them.

MrHen
  • 2,420
  • 2
  • 25
  • 39
  • Thanks for all the responses. There is still 1 ambiguity. Doesn't the else statement include 2 expressions and need to be considered a block? – Clifton Hill Apr 13 '16 at 17:13
  • Any `if` or `else` statement without braces will only affect the _next_ statement. If you want two statements to go with the `else` than you need to include braces. Semicolons are irrelevant for this. So, the answer to the question in your comment is yes: An else statement with two expressions is considered a block and _must_ have braces. – MrHen Apr 13 '16 at 17:32
  • Thanks again, but unless I'm mistaken the else statement above has 2 expressions and still works (in Chrome). Is this one of those technically-it's -fine, but don't do it in actual practice cuz it's lazy kind of things? – Clifton Hill Apr 13 '16 at 21:32
  • You are mistaken. The only statement matched with the `else` block is `step=0`. – MrHen Apr 13 '16 at 23:17