summaryrefslogtreecommitdiffhomepage
path: root/javascript.html.markdown
blob: 826fe7cde8204103f863e2837eea0250f30b7eed (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
---
language: javascript
author: Adam Brenecki
author_url: http://adam.brenecki.id.au
---

Javascript was created by Netscape's Brendan Eich in 1995. It was originally
intended as a simpler scripting language for web apps, complimenting Java for
more complex ones, but has become far more widely used than Java on the web.

Feedback would be highly appreciated! You can reach me at
[@adambrenecki](https://twitter.com/adambrenecki), or
[adam@brenecki.id.au](mailto:adam@brenecki.id.au).

```javascript
// Comments are like C. Single-line comments start with two slashes,
/* and multiline comments start with slash-star
   and end with star-slash */

// Statements can be terminated by ;
doStuff();

// ... but they don't have to be, as semicolons are automatically inserted
// wherever there's a newline, except in certain cases.
doStuff()

// Semicolons are a heated topic in the JavaScript world, but they're really a
// matter of personal or style-guide preference. We'll leave them off here.

/***********
 * 1. Primitive Datatypes and Operators
 ***********/

// Javascript has one number type that covers ints and floats.
3 // = 3
1.5 // = 1.5

// which support all the operations you'd expect.
1 + 1 // = 2
8 - 1 // = 7
10 * 2 // = 20
35 / 5 // = 7

// Uneven division works how you'd expect, too.
5 / 2 # = 2.5

// Enforce precedence with parentheses
(1 + 3) * 2 // = 8

// There's also a boolean type.
true
false

// Strings are created with ' or ".
'abc'
"Hello, world"

// Negation uses the ! symbol
!true // = false
!false // = true

// Equality is ==
1 == 1 // = true
2 == 1 // = false

// Inequality is !=
1 != 1 // = false
2 != 1 // = true

// More comparisons
1 < 10 #=> True
1 > 10 #=> False
2 <= 2 #=> True
2 >= 2 #=> True

// Strings are concatenated with +
"Hello " + "world!" // = "Hello world!"

// and are compared with < and >
"a" < "b" // = true

// You can also compare strings with numbers
"5" == 5 // = true

// but this is almost always not what you want, so use === to stop this
"5" === 5 // = false

// You can access characters in a string with charAt
"This is a string".charAt(0)

// There's also a null keyword
null // = null

/***********
 * 2. Variables and Lists
 ***********/

// variables are declared with the var keyword
var some_var = 5

// if you leave them off, you won't get an error...
some_other_var = 10

// but your variable will always end up with the global scope, even if it wasn't
// defined there, so don't do it.

/***********
 * 3. Control Structures
 ***********/

/***********
 * 4. Objects
 ***********/

/***********
 * 5. Functions, Scope and Closures
 ***********/

/***********
 * 6. Constructors and Prototypes
 ***********/
```