Skip to content

Commit 02e795c

Browse files
committed
Reorder JS chapters
1 parent ff95b87 commit 02e795c

File tree

5 files changed

+110
-107
lines changed

5 files changed

+110
-107
lines changed

astro.config.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import starlight from '@astrojs/starlight';
44

55
export const guideChapters = [
66
{ label: 'Motivation: Why learn HTML and CSS?', slug: 'guides/why-html-css' },
7-
{ label: 'About JavaScript', slug: 'guides/about-javascript' },
87
{ label: 'Setup: GitHub and VS Code for the Web', slug: 'guides/setup' },
98
{ label: 'Start with HTML', slug: 'guides/html' },
109
{ label: 'Style with CSS', slug: 'guides/css' },
11-
{ label: 'JavaScript for pages with shared components', slug: 'guides/javascript-to-render-multiple-pages-with-shared-components' },
10+
{ label: 'Introducing JavaScript', slug: 'guides/javascript' },
11+
{ label: 'Multiple pages with shared components', slug: 'guides/multiple-pages-with-shared-components' },
1212
{ label: 'A static blog from markdown files', slug: 'guides/static-blog-from-markdown-files' },
1313
]
1414

src/content/docs/guides/about-javascript.md

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/content/docs/guides/javascript.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
title: Introducing JavaScript
3+
template: splash
4+
---
5+
6+
Now that you got a taste of HTML and CSS, let's talk about JavaScript. It's the third language that the browser understands.
7+
8+
9+
## Client-side and server-side JavaScript
10+
11+
Inside the browser, JavaScript is best used sparingly to add interactivy to a page. Although unfortunately, some websites load millions of lines of JavaScript into the browser. Since a browser is also known as a _client_, this is known as client-side JavaScript. In fact, client-side JavaScript frameworks like React compute the whole page in the browser of your user every time they load the page. This may be required for an app like Google Docs or Visual Studio Code. But for most websites, it just makes everything slow for no good reason – especially on mobile phones, with their limited computing power and slow networks.
12+
13+
However, JavaScript is a general-purpose programming language like any other. Nowadays, you can run JavaScript also on your server or laptop, generate the HTML there, and only send that to your user's browser – just like people have done with PHP or Ruby for ages. This is known as server-side JavaScript and it's what we'll be doing primarily in this guide.
14+
15+
16+
## Introducing JavaScript
17+
18+
On your published website, open your [browser's developer tools](https://developer.mozilla.org/en-US/docs/Learn_web_development/Howto/Tools_and_setup/What_are_browser_developer_tools#how_to_open_the_devtools_in_your_browser) again. But this time, switch to the tab named **Console**.
19+
20+
![](https://developer.chrome.com/static/docs/devtools/console/javascript/image/the-console-99991743a015_2880.png)
21+
22+
This is an interactive JavaScript console, which means that you can type in JavaScript, hit `enter`, and it will calculate the result. Try adding two numbers by typing:
23+
24+
```js title=Console
25+
1 + 2
26+
```
27+
28+
After hitting `enter`, a new line will be shown with the result: `3`.
29+
30+
A piece of text, that a program works with, is called a _string_. In JavaScript, strings are usually wrapped in either single quotes or double quotes. Type:
31+
32+
```js title=Console
33+
'1' + "2 cats"
34+
```
35+
36+
and hit `enter`. It returns `"12 cats"` (this time with quotes). That's because when working on strings (as opposed to numbers), the `+` operator concatenates them together.
37+
38+
If you want your program to remember something for later, assign it to a variable. Create the variable `myName` and assign it the string `"Peter"` by typing:
39+
40+
```js title=Console
41+
const myName = "Peter";
42+
```
43+
44+
The semicolon at the end is customary to mark the end of a statement. Statements (as opposed to expressions) will not directly give a result – that's why it prints a line saying `undefined`.
45+
46+
A third way to write a string is to wrap it in backticks. This allows you to put variables (and other expressions) in it using `${ }`:
47+
48+
```js title=Console
49+
`I am ${myName} and ${3 * 10} years old`
50+
```
51+
52+
If you want to execute the same kind of computation multiple times, you need a function. One way to write a function that returns the string `"Hello World"`, and assign that function to the variable `hello` is:
53+
54+
```js title=Console
55+
const hello = () => "Hello World";
56+
```
57+
58+
Again, assignments always return `undefined`. But now you can call the function:
59+
60+
```js title=Console
61+
hello()
62+
```
63+
64+
which will return the string `"Hello World"`.
65+
66+
To make it a bit more useful, we can write a new function that takes an argument, which you can name as you wish. Let's call our argument `name`:
67+
68+
```js title=Console
69+
const helloName = (name) => `Hello ${name}`;
70+
```
71+
72+
You can call it like:
73+
74+
```js title=Console
75+
helloName("Peter")
76+
```
77+
78+
Finally, let's give you a sneak peak on JavaScript objects (which hold key-value pairs):
79+
80+
```js title=Console
81+
const person = { firstName: "Arthur", lastName: "Dent", age: 42 };
82+
`${person.firstName} ${person.lastName} is ${person.age} years old.`
83+
```
84+
85+
And arrays, which act like lists:
86+
87+
```js title=Console
88+
const shoppingList = ["bread", "milk", "butter"];
89+
const rememberFn = (item) => `Remember the ${item}!`;
90+
shoppingList.map(rememberFn)
91+
```
92+
93+
`.map()` calls the method `map` on the `shoppingList`. A method is a function attached to an object. All arrays come with a `map` method. When you call it, you need to give it a special kind of argument: a function.
94+
95+
Feel free to toy around a bit more in the JavaScript console. You can always reload the page to reset everything, meaning you'll lose all your variables.
96+
97+
The good news is that with JavaScript – like with any general-purpose programming language – you can create arbitrarily complex programs. That's also the bad news btw. Either way, this crash course should be enough for you to create that second page for your website.
98+
99+
:::tip[Want to learn more JavaScript?]
100+
[MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide) also has good documentation on JavaScript. But especially if you're new to programming in general, I can highly recommend the book [Eloquent JavaScript](https://eloquentjavascript.net/), which is free to read online.
101+
:::
Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,12 @@
11

22
---
3-
title: Use JavaScript to generate multiple pages with shared components
3+
title: Generate multiple pages with shared components
44
template: splash
55
---
66

77
In this chapter, you create a second page, and move the parts that are the same for both pages into shared components. You use a bit of server-side JavaScript to do that.
88

99

10-
## Introducing JavaScript
11-
12-
On your published website, open your [browser's developer tools](https://developer.mozilla.org/en-US/docs/Learn_web_development/Howto/Tools_and_setup/What_are_browser_developer_tools#how_to_open_the_devtools_in_your_browser) again. But this time, switch to the tab named **Console**.
13-
14-
![](https://developer.chrome.com/static/docs/devtools/console/javascript/image/the-console-99991743a015_2880.png)
15-
16-
This is an interactive JavaScript console, which means that you can type in JavaScript, hit `enter`, and it will calculate the result. Try adding two numbers by typing:
17-
18-
```js title=Console
19-
1 + 2
20-
```
21-
22-
After hitting `enter`, a new line will be shown with the result: `3`.
23-
24-
A piece of text, that a program works with, is called a _string_. In JavaScript, strings are usually wrapped in either single quotes or double quotes. Type:
25-
26-
```js title=Console
27-
'1' + "2 cats"
28-
```
29-
30-
and hit `enter`. It returns `"12 cats"` (this time with quotes). That's because when working on strings (as opposed to numbers), the `+` operator concatenates them together.
31-
32-
If you want your program to remember something for later, assign it to a variable. Create the variable `myName` and assign it the string `"Peter"` by typing:
33-
34-
```js title=Console
35-
const myName = "Peter";
36-
```
37-
38-
The semicolon at the end is customary to mark the end of a statement. Statements (as opposed to expressions) will not directly give a result – that's why it prints a line saying `undefined`.
39-
40-
A third way to write a string is to wrap it in backticks. This allows you to put variables (and other expressions) in it using `${ }`:
41-
42-
```js title=Console
43-
`I am ${myName} and ${3 * 10} years old`
44-
```
45-
46-
If you want to execute the same kind of computation multiple times, you need a function. One way to write a function that returns the string `"Hello World"`, and assign that function to the variable `hello` is:
47-
48-
```js title=Console
49-
const hello = () => "Hello World";
50-
```
51-
52-
Again, assignments always return `undefined`. But now you can call the function:
53-
54-
```js title=Console
55-
hello()
56-
```
57-
58-
which will return the string `"Hello World"`.
59-
60-
To make it a bit more useful, we can write a new function that takes an argument, which you can name as you wish. Let's call our argument `name`:
61-
62-
```js title=Console
63-
const helloName = (name) => `Hello ${name}`;
64-
```
65-
66-
You can call it like:
67-
68-
```js title=Console
69-
helloName("Peter")
70-
```
71-
72-
Finally, let's give you a sneak peak on JavaScript objects (which hold key-value pairs):
73-
74-
```js title=Console
75-
const person = { firstName: "Arthur", lastName: "Dent", age: 42 };
76-
`${person.firstName} ${person.lastName} is ${person.age} years old.`
77-
```
78-
79-
And arrays, which act like lists:
80-
81-
```js title=Console
82-
const shoppingList = ["bread", "milk", "butter"];
83-
const rememberFn = (item) => `Remember the ${item}!`;
84-
shoppingList.map(rememberFn)
85-
```
86-
87-
`.map()` calls the method `map` on the `shoppingList`. A method is a function attached to an object. All arrays come with a `map` method. When you call it, you need to give it a special kind of argument: a function.
88-
89-
Feel free to toy around a bit more in the JavaScript console. You can always reload the page to reset everything, meaning you'll lose all your variables.
90-
91-
The good news is that with JavaScript – like with any general-purpose programming language – you can create arbitrarily complex programs. That's also the bad news btw. Either way, this crash course should be enough for you to create that second page for your website.
92-
93-
:::tip[Want to learn more JavaScript?]
94-
MDN also has good documentation on JavaScript. But especially if you're new to programming in general, I can highly recommend the book [Eloquent JavaScript](https://eloquentjavascript.net/), which is free to read online.
95-
:::
96-
97-
9810
## A second page
9911

10012
So far your website still consists of only a single page: the home page. Add a second page by creating a new file: `routes/news.html`. You _could_ add the same `header` and `footer` all over again in this second file. But the more pages you add, the more tedious this approach becomes. And when you modify the header or footer in one file, it's easy to forget changing all other files. The solution is to move the header and footer to their own reusable _components_.

src/content/docs/guides/setup.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ Now that you're on your repository's page on GitHub:
2929
2. When prompted about signing in using GitHub, click the _Allow_ button.
3030
3. Finally, you may have to hit `enter` to select your GitHub user.
3131

32-
Now, install the following two VS Code extensions:
32+
## Mastro
3333

34-
- [Mastro](https://marketplace.visualstudio.com/items?itemName=mastro.mastro-vscode-extension) to preview and publish your work, and
34+
We'll use the _Mastro_ static site generator (SSG). Mastro is special in that it can also run as a VS Code extension in your browser. That way you don't have to install a runtime like Node.js or Deno on your computer, and you don't have to mess around with the terminal and command line.
35+
36+
Install the following two VS Code extensions:
37+
38+
- [Mastro](https://marketplace.visualstudio.com/items?itemName=mastro.mastro-vscode-extension), and
3539
- [FAST Tagged Template Literals](https://marketplace.visualstudio.com/items?itemName=ms-fast.fast-tagged-templates) to properly highlight HTML code inside JavaScript templates.
3640

3741
![](../../../assets/vscode-extensions.png)

0 commit comments

Comments
 (0)