Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is it appropriate to use a semicolon?

I know that JavaScript (and thus TypeScript) support the omission of semicolons in many cases. Nevertheless I want to add semicolons to be unambiguous as suggested in TypeScript Deep Dive

However I cannot find a guide that lists where to use semicolon. For example look at the following code

class Person {   private name: string; // A    constructor(name: string) {     this.name = name;   }; // B    public add = () => {     return "C";   }; // C }; // D 

I'm fairly sure to use a semicolon at A. But what about B, C, D and all the other cases not covered by my example?

I'm not asking where to omit semicolon but where to add them. An answer like always does not fulfill my needs since I cannot add a ; after public. I want to know where exactly to put semicolon.

like image 550
ooxi Avatar asked Aug 08 '16 07:08

ooxi


People also ask

When should a semicolon be used examples?

A semicolon may be used between independent clauses joined by a connector, such as and, but, or, nor, etc., when one or more commas appear in the first clause. Example: When I finish here, and I will soon, I'll be glad to help you; and that is a promise I will keep.

What is a semicolon example?

When to Use a Semicolon. A semicolon (;) is a punctuation mark that has two main functions: Semicolons separate items in a complex list. For example, The Council is comprised of ten members: three from Sydney, Australia; four from Auckland, New Zealand; two from Suva, Fiji; and one from Honiara, Solomon Islands.

When should a semicolon be used instead of a comma?

The semicolon is used when connecting two sentences or independent clauses. Unlike the comma, you do not use coordinating conjunctions, e.g., and, or, but, etc. A semicolon can also be used when connecting two independent clauses with conjunctive adverbs, e.g., however, therefore, thus, otherwise, etc.


1 Answers

Just prefix lines starting with [, (, or ` with a semicolon and you're (almost) golden*

Using the same example as another answer:

var x = { xx : "hello", yy : "world"} (function () {     console.log("Hello World"); })(); 

We add a semicolon according to this rule:

var x = { xx : "hello", yy : "world"} ;(function () { 

otherwise javascript thinks we're trying to call( some function, or reference[ some array. This is simpler, easier to follow, and it's visually easier to spot. You also need semicolons in for loops, but the .forEach method is a cleaner and easier method. I'd confidently say this one rule covers 99% of the scenarios you need to use a semicolon in javascript/typescript.

Following this method, it's important to associate a newline with terminating a statement.

*This returns the venerable undefined:

  return            7 

After return, there's a newline, and the browser inserts a semicolon, terminating the statement like this:

  return; // this will return undefined.           7 

Do this instead:

  return (           7   ) 

Javascript is actually pretty smart with semicolons, there's an open paren, so no semicolon is inserted until the closing paren is found.

If you have a habit of putting semicolons everywhere and not knowing exactly when they are needed, you could read this for a several page long explanation: http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding

I admit most people will still just litter semi colons at the end of every line, but if you're new and just learning, this is the better approach.

like image 188
Devin Rhode Avatar answered Sep 21 '22 09:09

Devin Rhode