• # Hommage á Kernighan and Ritchie

The only way to learn a new programming language is by writing programs in it. The first program to write is the same for all languages:

Print the words
`hello, world`

This is the big hurdle; to be able to leap over it you have to be able to create the program text somewhere, compile it successfully, load it, run it, and find out where your output went. With these mechanical details mastered, everything else is comparatively easy.

• # Tell Robert What To Do

1. Store the number 0 in memory location 0.
2. Store the number 1 in memory location 1
3. Store the value of memory location 1 in memory location 2.
4. Subtract the number 11 from the value in memory location 2.
5. If the value in memory location 2 is the number 0, go to instruction 9.
6. Add the value of memory location 1 to memory location 0.
7. Add the number 1 to the value of memory location 1.
8. Continue with instruction 3.
9. Output the value of memory location 0.
• # Robert's Code

```00110001 00000000 00000000
00110001 00000001 00000001
00110011 00000001 00000010
01010001 00001011 00000010
00100010 00000010 00001000
01000011 00000001 00000000
01000001 00000001 00000001
00010000 00000010 00000000
01100010 00000000 00000000```
• # Young Robert's Version

```Set 'total' to 0
Set 'count' to 1
[loop]
Set 'compare' to 'count'
Subtract 11 from 'compare'
If 'compare' is zero, continue at [end]
Continue at [loop]
[end]
Output 'total'```
• # The JavaScript Way

```let total = 0;
let count = 1;
while (count <= 10) {
total += count;
count++;
}
console.log(total);```
• # Another JavaScript Way

```let sumRange = function(first, last) {
if (last === first) {
return first;
}
return last + sumRange(first, last - 1);
}
console.log(sumRange(1, 10));```
• # The Previous 5 Slides

• Pseudo coded a problem
• Stone age coded it in machine code
• Modernized it into some 3rd gen language
• Showed two different JavaScript implementations
• # The JavaScript Taught

• sequence, iteration, and selection
• declaration of variables and assignment of values
• arithmetic, comparisons, and operators
• creation of, invoking, and returning from a function
• recursion
• # What?

• That is a hell of a lot in five slides
• I bet you have more questions than answers now

• numbers
• strings
• Booleans
• objects
• functions
• undefined
• # Numbers

• All numbers are 64-bit variables, but
• All numbers are floating point with 11 bits for decimals
• Integer, full precision range -(253-1) - 253-1
• -9007199254740991 - 9007199254740991
• Floating point, 56.1629 or -1.4654
• Scientific notation eg 5.9723e+24
• # Strings

• "The quick brown fox jumps over the lazy dog"
• 'The quick brown fox jumps over the lazy dog'
• `The quick brown fox jumps over the lazy dog`
• "You Ain't Nothin' But A Hound Dog"
• '<li class=\'slide\'>'
• 'This is the first line\nAnd this is the second'
• `This is the first line
And this is the second`
• 'con' + 'cat' + 'e' + 'nate' becomes 'concatenate'
• # Operators

• Binary
• +, -, *, /
• %, **
• ++, --
• Unary
• typeof x;
• -y, +42
• Comparators
• <, <=, ==, ===, >=, >
• !, !=, !==
• # Keeping State

• Keeping state means remembering values
• To remember values programmers place them in variables
• If
```'use strict';
var i = 42;
let s = 'Here\'s the beef';
```
• You must define variables with `let` or `var`
• # References

The book: https://eloquentjavascript.net/ Read it online or buy the 2nd edition paperback

My bachelor course material: http://x15.dk/webdev/site/pt05.xhtml

This presentation available from