-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBasics of Programming with JavaScript
75 lines (61 loc) · 2.46 KB
/
Basics of Programming with JavaScript
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
JavaScript Introduction
Introduction
Kyle's Work
Introduction to Programming
You don't know JS: up & going
Source code is not really for computer - multiple source codes can be identical computer instruction
Source code is for developer to read and understand what the computer is doing
Avoid unfounded assumptions based on memory - aim for sensible code
Syntax + Grammer (rules) = how to write JS programs as "statements"
Statements
Ex. a = b * 2; // In JS, statements ends with semicolon
2 is a literal value
* is the muliplication operator
A word without special meaning (keyword) is a variable, an identifier - like a or b
left side of assignment (=) is computed and assigned to the left hand side
Expressions
Statements are composed of phrases, known as expressions
For ex. in a = ((b) * (2));
a is an expression
b is an expression
2 is an expression
b * 2 is an expression
a = b * 2 is an assignment expression and a statement
a = b * 2 + foo(c * 3);
((a) = (((b) * (2)) + ((foo)((c) * (3))));
adding parentheses can improve readability
operation order typically goes from left to right, so b * 2 before foo
except when it is overridden by operator precedence or associativity
In JS, 2, 2.0 are the same value
Executing a Program
Turn source code into computer instruction
JavaScript engine turn JS source code to the next layer down
JavaScript compiles on the fly and run the compiled code immediately - Error caught early on
Practice Coding
JS has both static error and run-time error
JS catches syntax error by compiling code first then executing the compiled code
shift + enter for multiline input on console
Input and Output
alert("Hello World"); // pop-up with Hello World
alert itself is not part of JS, but part of the browser
console.log("Hello World"); // another simple way for output
The "< undefined" that usually at the end of execution:
console environment takes last statement and evaluate for a value, then show that value
ex. a = 2; // the result of assignment expression is the value assigned, so 2 is returned
ex. var b = 2; // a var staement has no return value so undefined is returned
Another way to get input: prompt()
ex. var age = prompt("What is your age?");
prompt() returns a string represents what is typed
2
JavaScript Syntax
Operators
Values and Types
Code Comments
Variables and Blocks
Conditional Statements
Loops
Functions
Scope
Challenge 1
Challenge 1 Solution
ECMAScript 6