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
- ...Lisäksi on muitakin kutsumanimiä kuten "Chakra" IE:lle, "JavaScriptCore", "Nitro" ja "SquirrelFish" Safarille, jne.
30
30
31
-
Yllä luetellut termit on hyvä muistaa, koska niitä käytetään internetin sovelluskehitykseen liittyvissä artikkeleissa. Me tulemme myös käyttämään niitä. Jos esimerkiksi "V8 tukee toimintoa X", se todennäköisesti toimii Chromessaja Operassa.
31
+
Yllä luetellut termit on hyvä muistaa, koska niitä käytetään internetin sovelluskehitykseen liittyvissä artikkeleissa. Me tulemme myös käyttämään niitä. Jos esimerkiksi "V8 tukee toimintoa X", se todennäköisesti toimii Chromessa, Operassa ja Edgessä.
32
32
33
33
```smart header="Miten moottorit toimivat?"
34
34
@@ -59,7 +59,7 @@ Selaimessa JavaScript voi esimerkiksi:
59
59
60
60
## Mitä JavaScript EI VOI tehdä selaimessa?
61
61
62
-
JavaScriptin kyvyt selaimessa on rajoitettu käyttäjän turvallisuuden vuoksi. Tavoitteena on estää pahaa nettisivua pääsemästä käsiksi yksityisiin tietoihin tai vahingoittamasta käyttäjän tietoja.
62
+
JavaScriptin kykyjä selaimessa on rajoitettu käyttäjän turvallisuuden vuoksi. Tavoitteena on estää pahaa nettisivua pääsemästä käsiksi yksityisiin tietoihin tai vahingoittamasta käyttäjän tietoja.
63
63
64
64
Esimerkkejä tällaisista rajoituksista ovat:
65
65
@@ -117,6 +117,6 @@ Lisääkin esimerkkejä löytyy. Silti, vaikka käytämme jotain muunnettavaa ki
117
117
118
118
## Yhteenveto
119
119
120
-
- JavaScript suunniteltiin alunperin vain selaimeen, mutta sitä käytetään nykyisin myös monissa muissa ympäristöissä.
120
+
- JavaScript suunniteltiin alunperin vain selaimessa käytettäväksi kieleksi, mutta sitä käytetään nykyisin myös monissa muissa ympäristöissä.
121
121
- JavaScriptillä on nykyisin uniikki asema laajimmin käytettynä selainkielenä, joka integroituu täysin HTML:ään ja CSS:ään.
122
122
- Monet JavaScriptiksi "muunnettavat" kielet tuovat mukanaan tiettyjä toimintoja. On suositeltavaa tutustua niihin, ainakin lyhyesti, kun JavaScript on hallussa.
Yllä, `/polku/tiedostoon/script.js` tarkoittaa absoluuttista tiedostopolkua skriptiin sivuston juuresta (root). Polun voi kirjoittaa myös nykyisen sivun suhteen. Esimerkiksi `src="script.js"` tarkoittaisi tiedostoa nimeltä `"script.js"` nykyisessä kansiossa.
78
+
=======
79
+
Here, `/path/to/script.js` is an absolute path to the script from the site root. One can also provide a relative path from the current page. For instance, `src="script.js"`, just like `src="./script.js"`, would mean a file `"script.js"` in the current folder.
80
+
>>>>>>> 291b5c05b99452cf8a0d32bd32426926dbcc0ce0
77
81
78
82
Voimme kirjoittaa myös koko URL-osoitteen. Esimerkiksi:
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/05-types/article.md
+18-13Lines changed: 18 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,13 +46,15 @@ Besides regular numbers, there are so-called "special numeric values" which also
46
46
alert( "not a number" / 2 ); // NaN, such division is erroneous
47
47
```
48
48
49
-
`NaN` is sticky. Any further operation on `NaN` returns `NaN`:
49
+
`NaN` is sticky. Any further mathematical operation on `NaN` returns `NaN`:
50
50
51
51
```js run
52
-
alert( "not a number" / 2 + 5 ); // NaN
52
+
alert( NaN + 1 ); // NaN
53
+
alert( 3 * NaN ); // NaN
54
+
alert( "not a number" / 2 - 1 ); // NaN
53
55
```
54
56
55
-
So, if there's a `NaN` somewhere in a mathematical expression, it propagates to the whole result.
57
+
So, if there's a `NaN` somewhere in a mathematical expression, it propagates to the whole result (there's only one exception to that:`NaN ** 0` is `1`).
56
58
57
59
```smart header="Mathematical operations are safe"
58
60
Doing maths is "safe" in JavaScript. We can do anything: divide by zero, treat non-numeric strings as numbers, etc.
@@ -213,14 +215,7 @@ The `symbol` type is used to create unique identifiers for objects. We have to m
213
215
214
216
The `typeof` operator returns the type of the argument. It's useful when we want to process values of different types differently or just want to do a quick check.
215
217
216
-
It supports two forms of syntax:
217
-
218
-
1. As an operator: `typeof x`.
219
-
2. As a function: `typeof(x)`.
220
-
221
-
In other words, it works with parentheses or without them. The result is the same.
222
-
223
-
The call to `typeof x` returns a string with the type name:
218
+
A call to `typeof x` returns a string with the type name:
The last three lines may need additional explanation:
252
247
253
248
1.`Math` is a built-in object that provides mathematical operations. We will learn it in the chapter <info:number>. Here, it serves just as an example of an object.
254
-
2. The result of `typeof null` is `"object"`. That's an officially recognized error in `typeof` behavior, coming from the early days of JavaScript and kept for compatibility. Definitely, `null` is not an object. It is a special value with a separate type of its own.
249
+
2. The result of `typeof null` is `"object"`. That's an officially recognized error in `typeof`, coming from very early days of JavaScript and kept for compatibility. Definitely, `null` is not an object. It is a special value with a separate type of its own. The behavior of `typeof` is wrong here.
255
250
3. The result of `typeof alert` is `"function"`, because `alert` is a function. We'll study functions in the next chapters where we'll also see that there's no special "function" type in JavaScript. Functions belong to the object type. But `typeof` treats them differently, returning `"function"`. That also comes from the early days of JavaScript. Technically, such behavior isn't correct, but can be convenient in practice.
256
251
252
+
```smart header="The `typeof(x)` syntax"
253
+
You may also come across another syntax: `typeof(x)`. It's the same as `typeof x`.
254
+
255
+
To put it clear: `typeof` is an operator, not a function. The parentheses here aren't a part of `typeof`. It's the kind of parentheses used for mathematical grouping.
256
+
257
+
Usually, such parentheses contain a mathematical expression, such as `(2 + 2)`, but here they contain only one argument `(x)`. Syntactically, they allow to avoid a space between the `typeof` operator and its argument, and some people like it.
258
+
259
+
Some people prefer `typeof(x)`, although the `typeof x` syntax is much more common.
260
+
```
261
+
257
262
## Summary
258
263
259
264
There are 8 basic data types in JavaScript.
@@ -269,7 +274,7 @@ There are 8 basic data types in JavaScript.
269
274
270
275
The `typeof` operator allows us to see which type is stored in a variable.
271
276
272
-
-Two forms: `typeof x` or`typeof(x)`.
277
+
- Usually used as `typeof x`, but `typeof(x)` is also possible.
273
278
- Returns a string with the name of the type, like `"string"`.
274
279
- For `null` returns `"object"` -- this is an error in the language, it's not actually an object.
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/08-operators/article.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -194,22 +194,22 @@ Here's an extract from the [precedence table](https://developer.mozilla.org/en-U
194
194
| Precedence | Name | Sign |
195
195
|------------|------|------|
196
196
|...|...|...|
197
-
|17| unary plus |`+`|
198
-
|17| unary negation |`-`|
199
-
|16| exponentiation |`**`|
200
-
|15| multiplication |`*`|
201
-
|15| division |`/`|
202
-
|13| addition |`+`|
203
-
|13| subtraction |`-`|
197
+
|15| unary plus |`+`|
198
+
|15| unary negation |`-`|
199
+
|14| exponentiation |`**`|
200
+
|13| multiplication |`*`|
201
+
|13| division |`/`|
202
+
|12| addition |`+`|
203
+
|12| subtraction |`-`|
204
204
|...|...|...|
205
-
|3| assignment |`=`|
205
+
|2| assignment |`=`|
206
206
|...|...|...|
207
207
208
-
As we can see, the "unary plus" has a priority of`17` which is higher than the `13`of"addition" (binary plus). That's why, in the expression `"+apples + +oranges"`, unary pluses work before the addition.
208
+
As we can see, the "unary plus" has a priority of`15` which is higher than the `12`of"addition" (binary plus). That's why, in the expression `"+apples + +oranges"`, unary pluses work before the addition.
209
209
210
210
## Assignment
211
211
212
-
Let's note that an assignment `=` is also an operator. It is listed in the precedence table with the very low priority of`3`.
212
+
Let's note that an assignment `=` is also an operator. It is listed in the precedence table with the very low priority of`2`.
213
213
214
214
That's why, when we assign a variable, like `x = 2 * 2 + 1`, the calculations are done first and then the `=` is evaluated, storing the result in `x`.
215
215
@@ -437,7 +437,7 @@ The list of operators:
437
437
- RIGHT SHIFT ( `>>` )
438
438
- ZERO-FILL RIGHT SHIFT ( `>>>` )
439
439
440
-
These operators are used very rarely, when we need to fiddle with numbers on the very lowest (bitwise) level. We won't need these operators any time soon, as web development has little use of them, but in some special areas, such as cryptography, they are useful. You can read the [Bitwise Operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Bitwise) chapter on MDN when a need arises.
440
+
These operators are used very rarely, when we need to fiddle with numbers on the very lowest (bitwise) level. We won't need these operators any time soon, as web development has little use of them, but in some special areas, such as cryptography, they are useful. You can read the [Bitwise Operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#bitwise_operators) chapter on MDN when a need arises.
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/11-logical-operators/article.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -123,7 +123,7 @@ This leads to some interesting usage compared to a "pure, classical, boolean-onl
123
123
124
124
It means that `||` processes its arguments until the first truthy value is reached, and then the value is returned immediately, without even touching the other argument.
125
125
126
-
That importance ofthis feature becomes obvious if an operand isn't just a value, but an expression with a side effect, such as a variable assignment or a function call.
126
+
The importance ofthis feature becomes obvious if an operand isn't just a value, but an expression with a side effect, such as a variable assignment or a function call.
127
127
128
128
In the example below, only the second message is printed:
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/12-nullish-coalescing-operator/article.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@
4
4
5
5
The nullish coalescing operator is written as two question marks `??`.
6
6
7
-
As it treats `null` and `undefined` similarly, we'll use a special term here, in this article. We'll say that an expression is "defined" when it's neither `null` nor `undefined`.
7
+
As it treats `null` and `undefined` similarly, we'll use a special term here, in this article. For brevity, we'll say that a value is "defined" when it's neither `null` nor `undefined`.
8
8
9
9
The result of `a ?? b` is:
10
10
- if `a` is defined, then `a`,
@@ -22,9 +22,9 @@ result = (a !== null && a !== undefined) ? a : b;
22
22
23
23
Now it should be absolutely clear what `??` does. Let's see where it helps.
24
24
25
-
The common use case for `??` is to provide a default value for a potentially undefined variable.
25
+
The common use case for `??` is to provide a default value.
26
26
27
-
For example, here we show `user` if defined, otherwise `Anonymous`:
27
+
For example, here we show `user` if its value isn't `null/undefined`, otherwise `Anonymous`:
We can also use a sequence of `??` to select the first value from a list that isn't `null/undefined`.
44
44
45
-
Let's say we have a user's data in variables `firstName`, `lastName` or `nickName`. All of them may be not defined, if the user decided not to enter a value.
45
+
Let's say we have a user's data in variables `firstName`, `lastName` or `nickName`. All of them may be not defined, if the user decided not to fill in the corresponding values.
46
46
47
-
We'd like to display the user name using one of these variables, or show "Anonymous" if all of them aren't defined.
47
+
We'd like to display the user name using one of these variables, or show "Anonymous" if all of them are `null/undefined`.
48
48
49
49
Let's use the `??` operator for that:
50
50
@@ -106,11 +106,11 @@ In practice, the zero height is often a valid value, that shouldn't be replaced
106
106
107
107
## Precedence
108
108
109
-
The precedence of the `??` operator is about the same as `||`, just a bit lower. It equals `5` in the [MDN table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table), while `||` is `6`.
109
+
The precedence of the `??` operator is the same as `||`. They both equal `4` in the [MDN table](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table).
110
110
111
111
That means that, just like `||`, the nullish coalescing operator `??` is evaluated before `=` and `?`, but after most other operations, such as `+`, `*`.
112
112
113
-
So if we'd like to choose a value with `??`in an expression with other operators, consider adding parentheses:
113
+
So we may need to add parentheses in expressions like this:
114
114
115
115
```js run
116
116
let height =null;
@@ -128,7 +128,7 @@ Otherwise, if we omit parentheses, then as `*` has the higher precedence than `?
128
128
// without parentheses
129
129
let area = height ??100* width ??50;
130
130
131
-
// ...works the same as this (probably not what we want):
0 commit comments