dankoiDev
dankoiDev

Reputation: 123

Javascript Silly Question: if-statement syntax

There being a ' ; ' after one and not the other. I was wondering if there was a difference or additional functionality between these 2 if statements?

func(x){  // with a ; at end
    if (false){
        throw new Error('blah');
    };
}

func(x){  // without a ; at end
    if (false){
        throw new Error('blah');
    }
}

Upvotes: 1

Views: 51

Answers (2)

ICeZer0
ICeZer0

Reputation: 588

There is no differences or additional functionality between the 2 statements

Semicolons are optional in javascript, the interpreter will insert a semicolon at the end of a statement if needed.

In other programming languages like C, the semicolon denotes to the compiler the end of one instruction and the beginning of the next.

Upvotes: 0

Barmar
Barmar

Reputation: 782498

There's no difference.

You don't need a ; after a statement block. If you add one, it's just terminating an empty statement, which doesn't do anything.

The first version is probably a typo, it's not normal to put a ; there.

Upvotes: 2

Related Questions