When should I use ;


When should I use ;
And is it important


Super important! You need to use ; after a lot of things.

console.log( 'your text' );

var varName = function (parameters) {

if (parameter) {

There is more than that on JavaScript, but this is the basic. And remember: using ; after your code is very important.


Use the semicolon when you declare a variable and when you directly tell computer to do something right now.
For instance, there are two ways to create a function:

// 1
var func = function() {
    // code
// 2
function func() {
    // code

The first one directly orders computer to assign this function to the "func" variable, so we must put semicolon at the end. The second just tells computer: "There's such a function with a particular name and code", so no semicolon here.

When we call a function, we directly command computer to execute its code, so we need a semicolon.

func(); // Computer, execute the "func" function!

Another good example is the "if / else" statement.

if (condition1) {
} else {

This statement just makes computer understand what code to execute, so the semicolon isn't needed. When it understands, it starts executing specific direct commands which need the semicolon after them.

That's the way I understand all this stuff.


The first 2 are correct but not semicolons after ifs: