Semicolon


#1

In the language of JavaScript, when is the semicolon put to use? I have gotten an error saying that semicolons do not belong after 'if' statements, so I now know that they don't belong after those.


#2

@tonikpayc,
Have a read
http://javascript.crockford.com/code.html

Do a google search
== discussions / opinions ==
javascript when to use semicolon site:stackoverflow.com
= http://stackoverflow.com/questions/2399935/why-use-semicolon (also look in the linked column)
= http://stackoverflow.com/questions/17036135/why-is-it-that-semicolons-are-not-used-after-if-else-statements


#3

Semicolons aren't required due to the nature of JavaScript's automatic semicolon insertion. Here are the different ways to use semicolons in js:

  1. When the assignment operator is used outside of parentheses, insert a semicolon after the right operand of the assignment operator.
  2. Insert a semicolon after a closing parenthesis if a function is being invoked; however, not when it is the second operand of a ternary operator
  3. Insert a semicolon after one of these keywords and their corresponding syntax.
  4. Insert a semicolon after a variable declaration if these two conditions are true: 1) the variable is not a parameter or 2) the variable is the last declared variable in a list of variables
  5. Insert a semicolon after each of the first two arguments in a for-loop. This rule clearly breaks rule #1: Semicolons should not be used inside of parentheses.

#4

@josephgodwinke,

Could you expand your Post
by adding an EXAMPLE for every =point=...


#5

It be better if you could show us what you wrote in the console!


#6

WHEN TO INSERT SEMI-COLON IN JS

  1. Insert a semicolon after keywords and their corresponding syntax such as:

    return “greetings!”;  
    console.log("greetings!");
  2. When the assignment operator is used outside of parentheses, insert a semicolon after the right operand of the assignment operator

     //Use a semi-colon       
    var setAge = 'Ten';
    
            var myAge = function(myAge = “ten”) {  
              return myAge;        
            };
  3. Insert a semicolon after a closing parenthesis if a function is being invoked:

    // Use a semicolon
    console.log(“greetings!”);
  4. Insert a semicolon after a variable declaration if the variable is not a parameter:

var i;

  1. Insert a semicolon after each of the first two arguments in a for-loop such as:

    for (i = 5; i; i--) {  
      console.log(i);
    }