You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: data/guide/javascript.md
+31-14Lines changed: 31 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,49 +4,54 @@ title: Introducing JavaScript
4
4
5
5
Now that you got a taste of HTML and CSS, let's talk about JavaScript. It's the third language that the browser understands. One of the easiest ways to get your feet wet with JavaScript, is in your browser's console.
6
6
7
-
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. (Ideally on your published web page, but anywhere will do). But this time, switch to the tab named **Console**.
7
+
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. (Perhaps on your published web page, or right here on this one). But this time, switch to the tab named **Console**.
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:
11
+
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:
12
12
13
13
```js title=Console
14
14
1+2
15
15
```
16
16
17
17
After hitting `enter`, a new line will be shown with the result: `3`.
18
18
19
-
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:
19
+
A piece of text, that a program works with, is called a _string_. In JavaScript, **strings** need to be wrapped either in single quotes `'`, double quotes`"`, or backticks `` ` ``. Type:
20
20
21
21
```js title=Console
22
22
'1'+"2 cats"
23
23
```
24
24
25
25
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.
26
26
27
-
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:
27
+
If you want your program to remember something for later, assign it to a variable. Create the **variable**`myName` and assign the string `"Peter"`to the variable:
28
28
29
29
```js title=Console
30
30
constmyName="Peter";
31
31
```
32
32
33
-
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`.
33
+
You can see that it printed out `undefined`. That's because the assignment statement itself doesn't return anything (as opposed to expressions). The semicolon is optional, but customary to mark the end of a statement.
34
34
35
-
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 `${ }`:
35
+
The `const` at the beginning means you are declaring a **constant**: you cannot assign to this variable again within this part of the program (or within the same console session). You can try writing the above statement again, and you'll get the error:
36
+
37
+
> redeclaration of const myName
38
+
39
+
The third way to write a string is to wrap it in **backticks**. This allows you to put variables (and other expressions) in it using `${ }`:
36
40
37
41
```js title=Console
38
42
`I am ${myName} and ${3*10} years old`
39
43
```
40
44
45
+
41
46
## Functions
42
47
43
-
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:
48
+
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 as follows. (This is a so-called Arrow function, but there is an older [function syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions) as well.)
44
49
45
50
```js title=Console
46
51
consthello= () =>"Hello World";
47
52
```
48
53
49
-
Again, assignments always return `undefined`. But now you can call the function:
54
+
Since it's an assignment, it prints undefined into the console. But now you can call the function:
50
55
51
56
```js title=Console
52
57
hello()
@@ -67,31 +72,43 @@ helloName("Peter")
67
72
```
68
73
69
74
70
-
## Objects and arrays
75
+
## Objects
71
76
72
-
Finally, let's give you a sneak peak on JavaScript objects (which hold key-value pairs):
77
+
Finally, let's give you a sneak peak on JavaScript [objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer) (which hold key-value pairs):
`${person.firstName}${person.lastName} is ${person.age} years old.`
77
83
```
78
84
79
-
And arrays, which act like lists:
85
+
86
+
## Arrays
87
+
88
+
And a sneak peak of [arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Indexed_collections), which act like lists:
80
89
81
90
```js title=Console
82
91
constshoppingList= ["bread", "milk", "butter"];
92
+
83
93
constrememberFn= (item) =>`Remember the ${item}!`;
84
94
shoppingList.map(rememberFn)
85
95
```
86
96
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.
97
+
`.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. The last two lines could also have been written as a single line:
98
+
99
+
```js title=Console
100
+
shoppingList.map((item) =>`Remember the ${item}!`)
101
+
```
88
102
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.
103
+
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. This will allow you to declare them again with `const`.
90
104
91
105
92
106
## The power of programming
93
107
94
-
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. Either way, this crash course should be enough for you to read and write some basic JavaScript.
108
+
The good news is that with JavaScript – like with any general-purpose programming language – you can create arbitrarily complex programs. Since we're all human and usually make mistakes, that's also the bad news.
109
+
110
+
Either way, this crash course should be enough for you to read and write some basic JavaScript, and follow the rest of the guide.
0 commit comments