-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution
49 lines (34 loc) · 3.04 KB
/
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// SEMICOLON NEEDED,NONE NEEDED in JS
// The semicolon in JavaScript is used to separate statements, but it can be omitted if the statement is followed by a line break (or there’s only one statement in a {block}). A statement is a piece of code that tells the computer to do something. Here are the most common types of statements:
// var i; // variable declaration
// i = 5; // value assignment
// i = i + 1; // value assignment
// i++; // same as above
// var x = 9; // declaration & assignment
// var fun = function() {...}; // var decl., assignmt, and func. defin.
// alert("hi"); // function call
// All of these statements can end with a ; but none of them must. The semicolon is only obligatory when you have two or more statements on the same line:
// var i = 0; i++ // <-- semicolon obligatory
// // (but optional before newline)
// var i = 0 // <-- semicolon optional
// i++ // <-- semicolon optional
// You shouldn’t put a semicolon after a closing curly bracket }. The only exceptions are assignment statements, such as var obj = {};, see above.
// // NO semicolons after }:
// if (...) {...} else {...}
// for (...) {...}
// while (...) {...}
// // BUT:
// do {...} while (...);
// // function statement:
// function (arg) { /*do this*/ } // NO semicolon after }
// It won't harm to put a semicolon after the { } of an if statement (it will be ignored, and you might see a warning that it's unnecessary). But a semicolon where it doesn't belong (such as after the round (brackets) of an if, for, while, or switch statement) is a very bad idea:
// if (0 === 1); { alert("hi") }
// // equivalent to:
// if (0 === 1) /*do nothing*/ ;
// alert ("hi");
// This code will alert "hi", but not because 0 equals 1, but because of the semicolon. It makes JavaScript think that you have an empty statement there, and everything to the right of it is treated as no longer belonging to the if conditional and thus independent of it.
// [edit] Another important quirk: inside the () of a for loop, semicolons only go after the first and second statement, never after the third:
// for (var i=0; i < 10; i++) {/*actions*/} // correct
// for (var i=0; i < 10; i++;) {/*actions*/} // SyntaxError
// The JavaScript syntax proofing tool JSLint, which is built into the Codecademy code editor, does a pretty good job of finding unnecessary semicolons – or missing ones. It'll show you yellow warning triangles in code lines. Hovering the mouse over a triangle will tell you if there's a missing semicolon or an unnecessary one. You can generally trust those warnings until you develop an intuition of where to use semicolons and where not to.
// Some consider it a good habit to terminate each statement with a ; – that makes your code a little easier to parse, and to compress: if you remove line breaks you needn't worry about several statements ending up unseparated on the same line.