|
1 | 1 | ---
|
2 |
| -chapter: js101 |
3 |
| -section: 1 |
| 2 | +chapter: javascript-101 |
| 3 | +section: 2 |
4 | 4 | title: Syntax Basics
|
5 | 5 | attribution: jQuery Fundamentals
|
6 | 6 | github: jquery
|
7 | 7 | ---
|
8 |
| -Understanding statements, variable naming, whitespace, and other basic JavaScript syntax. |
9 | 8 |
|
10 |
| -<javascript caption="A simple variable declaration"> |
11 |
| -var foo = 'hello world'; |
12 |
| -</javascript> |
| 9 | +### Comments |
| 10 | + |
| 11 | +JavaScript has support for single and multi line comments. Comments are ignored by the JavaScript engine, therefore have no side-effects on the outcome of the program. They should be used to document the code for future developers (including yourself!). There are libraries available that can generate project documentation pages based on commenting conventions used, [JSDoc](http://code.google.com/p/jsdoc-toolkit/, "JSDoc Toolkit") is one of the more popular ones. |
| 12 | + |
| 13 | +<javascript caption="Single and multi line comments."> |
| 14 | +// this is an example of a single line comment. |
13 | 15 |
|
14 |
| -<javascript caption="Whitespace has no meaning outside of quotation marks"> |
15 |
| -var foo = 'hello world'; |
| 16 | +/* |
| 17 | + * this is an example |
| 18 | + * of a |
| 19 | + * multi line |
| 20 | + * comment. |
| 21 | + */ |
16 | 22 | </javascript>
|
17 | 23 |
|
18 |
| -<javascript caption="Parentheses indicate precedence"> |
19 |
| -2 * 3 + 5; // returns 11; multiplication happens first |
20 |
| -2 * (3 + 5); // returns 16; addition happens first |
| 24 | +### Whitespace |
| 25 | + |
| 26 | +Whitespace is also ignored in JavaScript. There are many tools that will actully strip out all the whitespace in a program reducing the overall file size in order to improve network latency. Given the availability of tools like these, whitespace should be leveraged to make the code as readible as possible. |
| 27 | + |
| 28 | +<javascript caption="Whitespace is insignifigant."> |
| 29 | +var hello = "Hello"; |
| 30 | +var world = "World!"; |
21 | 31 | </javascript>
|
22 | 32 |
|
23 |
| -<javascript caption="Identation enhances readability, but does not have any special meaning"> |
| 33 | +<javascript caption="Semantic whitespace promotes readibility."> |
| 34 | +// readible code is good! |
24 | 35 | var foo = function() {
|
25 |
| - console.log('hello'); |
| 36 | + for (var i = 0; i < 10; i++) { |
| 37 | + alert(i); |
| 38 | + } |
26 | 39 | };
|
| 40 | + |
| 41 | +foo(); |
| 42 | + |
| 43 | +// not so much! |
| 44 | +var foo=function(){for(var i=0;i<10;++){alert(i);}};foo(); |
| 45 | +</javascript> |
| 46 | + |
| 47 | +### Reserved Words |
| 48 | + |
| 49 | +There is a handfull of reserved words that cannot be used when declaring user defined variables and functions. Some of these are currently implemented, some for future use, and others for historical reasons. A list of words and in depth explaination can be found on the [MDN JavaScript Reference](https://developer.mozilla.org/en/JavaScript/Reference/Reserved_Words, "MDN Reserved Words.") |
| 50 | + |
| 51 | +### Identifiers |
| 52 | + |
| 53 | +Identifiers are used to name variables and functions with a unique name so they can subsequently be refererred to by that name; variables and functions will be discussed in a later chapter. The name of an identifier must follow a few rules: |
| 54 | + |
| 55 | +* Cannot be a reserved word. |
| 56 | +* Can only be composed of letters, numbers, dollar signs, and underscores. |
| 57 | +* The first character cannot be a number. |
| 58 | + |
| 59 | +It is best practice to name these identifiers to something that will make sense to other developers and to you later on. |
| 60 | + |
| 61 | +<javascript caption="Valid identifier names."> |
| 62 | +var myAwesomeVariable = 'a'; |
| 63 | +var myAwesomeVariable2 = 'b'; |
| 64 | +var my_awesome_variable = 'c'; |
| 65 | +var $my_AwesomeVariable = 'd'; |
| 66 | +var _my_awesome_variable_$ = 'e'; |
27 | 67 | </javascript>
|
| 68 | + |
| 69 | +While it is possible to run JavaScript in server-side environments, that is out of the scope for this section. |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
0 commit comments