WIP
33
1-js/01-getting-started/01-hello-javascript/article.md
Normal file
|
@ -0,0 +1,33 @@
|
|||
# Hello, JavaScript!
|
||||
|
||||
The programs in JavaScript are called *scripts*. They can be embedded right into HTML using `<script>` tag and run automatically as the page loads.
|
||||
|
||||
For example, this HTML-page shows the "Hello" message:
|
||||
|
||||
```html run
|
||||
<!doctype html>
|
||||
<script>
|
||||
alert("Hello!");
|
||||
</script>
|
||||
```
|
||||
|
||||
JavaScript can execute not only in a browser, but also on a server, or actually on any device that has a special program called [the JavaScript engine](https://en.wikipedia.org/wiki/JavaScript_engine).
|
||||
|
||||
Browsers have built-in JavaScript engines, so they can run scripts.
|
||||
|
||||
We can also run scripts using [Node.js](https://nodejs.org), it's commonly used to build server-side applications.
|
||||
|
||||
Depending on the environment, JavaScript may provide platform-specific functionality.
|
||||
|
||||
- In a web browser, JavaScript can manipulate the web-page, send network requests, show messages and so on.
|
||||
- Node.js allows to run a web-server.
|
||||
|
||||
...And so on. Even a coffee machine may include its own JavaScript engine, that could allow us to program its recipes.
|
||||
|
||||

|
||||
|
||||
In this tutorial we concentrate on the "core JavaScript", that's the same everywhere.**
|
||||
|
||||
After you learn it, you can go in any direction: learn browser functionality, how to write servers and so on.
|
||||
|
||||
Please turn the page to start learning JavaScript!
|
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" width="453" height="129" viewBox="0 0 453 129"><defs><style>@import url(https://fonts.googleapis.com/css?family=Open+Sans:bold,italic,bolditalic%7CPT+Mono);@font-face{font-family:'PT Mono';font-weight:700;font-style:normal;src:local('PT MonoBold'),url(/font/PTMonoBold.woff2) format('woff2'),url(/font/PTMonoBold.woff) format('woff'),url(/font/PTMonoBold.ttf) format('truetype')}</style></defs><g id="v2" fill="none" fill-rule="evenodd" stroke="none" stroke-width="1"><g id="javascript-engine.svg"><g id="noun_Server_1653092" fill="#000" fill-rule="nonzero" opacity=".812" transform="translate(366 15)"><path id="Shape" d="M13 86a6 6 0 016-6h16a6 6 0 016 6v2h13V0H0v88h13v-2zm37-2h-5.2a10 10 0 00-1.8-4h7v4zM4 4h46v72H4V4zm5.2 80H4v-4h7a10 10 0 00-1.8 4z"/><path id="Rectangle" d="M12 10h30v4H12zM12 20h30v4H12zM12 30h30v4H12zM12 40h30v4H12zM12 50h30v4H12zM27 63h8v4h-8z"/><circle id="Oval" cx="14" cy="65" r="2"/><circle id="Oval" cx="22" cy="65" r="2"/><circle id="Oval" cx="40" cy="65" r="2"/></g><g id="noun_Coffee-Machine_1997833" fill="#000" fill-rule="nonzero" opacity=".812" transform="translate(187 15)"><path id="Shape" d="M88.577 0H5.423C2.428 0 0 2.63 0 5.875V23.5c0 3.245 2.428 5.875 5.423 5.875v48.958C2.428 78.333 0 80.963 0 84.208v3.917C0 91.37 2.428 94 5.423 94h83.154C91.572 94 94 91.37 94 88.125v-3.917c0-3.244-2.428-5.875-5.423-5.875V29.375c2.995 0 5.423-2.63 5.423-5.875V5.875C94 2.63 91.572 0 88.577 0zm1.808 84.208v3.917c0 1.082-.81 1.958-1.808 1.958H5.423c-.998 0-1.808-.876-1.808-1.958v-3.917c0-1.081.81-1.958 1.808-1.958h83.154c.998 0 1.808.877 1.808 1.958zm-56.039-11.75V56.792c0-2.163 1.619-3.917 3.616-3.917h18.076c1.997 0 3.616 1.754 3.616 3.917v15.666c0 3.245-2.428 5.875-5.423 5.875H39.769c-2.995 0-5.423-2.63-5.423-5.875zm50.616 5.875h-23.5a9.973 9.973 0 001.102-1.958c5.444-.252 9.739-5.103 9.744-11.006V63.9c0-6.028-4.469-10.94-10.033-11.025-1.287-2.415-3.662-3.907-6.237-3.917H37.962c-3.994 0-7.231 3.507-7.231 7.834v15.666c0 2.119.634 4.18 1.807 5.875h-23.5V29.375h75.924v48.958zM68.692 63.9v1.47c-.004 3.446-2.29 6.392-5.423 6.99V56.89c3.14.599 5.428 3.557 5.423 7.01zm21.693-40.4c0 1.082-.81 1.958-1.808 1.958H5.423c-.998 0-1.808-.876-1.808-1.958V5.875c0-1.082.81-1.958 1.808-1.958h83.154c.998 0 1.808.876 1.808 1.958V23.5z"/></g><g id="noun_Browser_1327494" fill="#000" fill-rule="nonzero" opacity=".812" transform="translate(14 16)"><g id="Group"><path id="Shape" d="M86 .523c1.105 0 2 1.294 2 2.889V87.19c0 1.595-.895 2.889-2 2.889H2c-1.105 0-2-1.294-2-2.89V3.413C0 1.817.895.523 2 .523h84zm-2 5.778H4v11.194h80V6.301zm0 16.972H4v61.028h80V23.273z"/><path id="Shape" d="M78.75 8.106c1.515 0 2.781 1.85 2.781 4.018 0 2.167-1.235 4.017-2.781 4.017s-2.781-1.85-2.781-4.017c0-2.168 1.266-4.018 2.781-4.018zm0 2.257c-.686 0-1.219.864-1.219 1.76 0 .897.564 1.761 1.219 1.761.655 0 1.219-.864 1.219-1.76 0-.897-.533-1.76-1.219-1.76zM63.625 8.106c1.5 0 2.812 1.85 2.812 4.018 0 2.167-1.281 4.017-2.812 4.017-1.546 0-2.781-1.85-2.781-4.017 0-2.168 1.266-4.018 2.781-4.018zm0 2.257c-.651 0-1.188.864-1.188 1.76 0 .897.567 1.761 1.188 1.761.655 0 1.219-.864 1.219-1.76 0-.897-.533-1.76-1.219-1.76zM71.187 8.106c1.516 0 2.782 1.85 2.782 4.018 0 2.167-1.235 4.017-2.782 4.017-1.53 0-2.78-1.85-2.78-4.017 0-2.168 1.28-4.018 2.78-4.018zm0 2.257c-.686 0-1.218.864-1.218 1.76 0 .897.563 1.761 1.218 1.761.621 0 1.22-.864 1.22-1.76 0-.897-.569-1.76-1.22-1.76z"/></g></g><text id="javascript-engine" fill="#8A704D" font-family="OpenSans-Regular, Open Sans" font-size="14" font-weight="normal"><tspan x="26.376" y="62">javascript</tspan> <tspan x="35.447" y="81">engine</tspan></text><text id="javascript-engine" fill="#8A704D" font-family="OpenSans-Bold, Open Sans" font-size="18" font-weight="bold"><tspan x="349.225" y="59">javascript</tspan> <tspan x="363.204" y="83">engine</tspan></text><text id="javascript-engine" fill="#8A704D" font-family="OpenSans-Bold, Open Sans" font-size="14" font-weight="bold"><tspan x="199.064" y="59">javascript</tspan> <tspan x="209.937" y="78">engine</tspan></text></g></g></svg>
|
After Width: | Height: | Size: 4 KiB |
406
1-js/01-getting-started/02-basics/article.md
Normal file
|
@ -0,0 +1,406 @@
|
|||
|
||||
# Basic concepts
|
||||
|
||||
Let's take a piece of JavaScript code and discuss it line by line.
|
||||
|
||||
Please don't hesitate to open "read more" sections below. They aren't strictly required, but contain valueable information.
|
||||
|
||||
```js run
|
||||
"use strict";
|
||||
|
||||
let message = "Hello!";
|
||||
|
||||
alert(message);
|
||||
```
|
||||
|
||||
Click the "run" icon ▷ in the right-upper corner to see how it works.
|
||||
|
||||
Now we'll go over it line by line.
|
||||
|
||||
## Strict mode
|
||||
|
||||
The first line:
|
||||
|
||||
```js
|
||||
"use strict";
|
||||
```
|
||||
|
||||
The code starts with the special directive: `"use strict"`.
|
||||
|
||||
To understand what it means, let's make a short dive into the history of JavaScript.
|
||||
|
||||
JavaScript appeared many years ago, in 1995. For a long time, it evolved without compatibility issues. New features were added to the language while old functionality didn't change.
|
||||
|
||||
That had the benefit of never breaking existing code. But the downside was that any mistake or an imperfect decision made by JavaScript's creators got stuck in the language forever.
|
||||
|
||||
This was the case until 2009, when the 5th version of the standard appeared. It added new features to the language and modified some of the existing ones.
|
||||
|
||||
Now the important part.
|
||||
|
||||
**To keep the old code working, these modernizations are off by default.** We need to explicitly enable them with a special directive: `"use strict"`
|
||||
|
||||
With that directive on top the script runs in so-called "strict mode". Or, we'd better say "modern mode", because that's what it essentially is.
|
||||
|
||||

|
||||
|
||||
Some JavaScript features enable strict mode automatically, e.g. classes and modules, so that we don't need to write `"use strict"` for them. We'll cover these features later.
|
||||
|
||||
**Here, in the tutorial, we'll always use strict mode, unless explicitly stated otherwise.**
|
||||
|
||||
We're studying modern JavaScript after all. But you'll also see notes about how things work without `use strict`, just in case you come across an old script.
|
||||
|
||||
## Variables
|
||||
|
||||
A [variable](https://en.wikipedia.org/wiki/Variable_(computer_science)) is a "named storage" for data. We can use variables to store goodies, visitors, and so on.
|
||||
|
||||
The second line declares (creates) a variable with the name `message` and stores the string `"John"` in it:
|
||||
|
||||
```js
|
||||
let message = "Hello!";
|
||||
```
|
||||
|
||||
We could also split this line into two:
|
||||
|
||||
```js
|
||||
let message;
|
||||
message = "Hello!";
|
||||
```
|
||||
|
||||
Here, we first declare the variable with `let message`, and then assign the value.
|
||||
|
||||
We can easily grasp the concept of a "variable" if we imagine it as a "box" for data, with a uniquely-named sticker on it.
|
||||
|
||||
For instance, the variable `message` can be imagined as a box labeled `"message"` with the value `"Hello!"` in it:
|
||||
|
||||

|
||||
|
||||
We can put any value in the box.
|
||||
|
||||
We can also change it as many times as we want:
|
||||
```js
|
||||
let message;
|
||||
|
||||
message = "Hello!";
|
||||
|
||||
message = "World!"; // value changed
|
||||
```
|
||||
|
||||
When the value is changed, the old data is removed from the variable:
|
||||
|
||||

|
||||
|
||||
We can also declare multiple variables and copy data from one into another.
|
||||
|
||||
```js run
|
||||
let hello = "Hello!";
|
||||
|
||||
let message;
|
||||
|
||||
*!*
|
||||
// copy the value "Hello!" from hello into message
|
||||
message = hello;
|
||||
*/!*
|
||||
```
|
||||
|
||||
Now we have two variables, both store the same string:
|
||||
|
||||

|
||||
|
||||
````warn header="Re-declaration triggers an error"
|
||||
A variable can be declared only once.
|
||||
|
||||
A repeated declaration of the same variable is an error:
|
||||
|
||||
```js run
|
||||
let message = "One";
|
||||
|
||||
// repeated 'let' leads to an error
|
||||
let message = "Two"; // SyntaxError: 'message' has already been declared
|
||||
```
|
||||
So, we should declare a variable once and then refer to it without `let`.
|
||||
````
|
||||
|
||||
````warn header="Omitting `let` is possible without `use strict`"
|
||||
In the old times, it was possible to create a variable by a mere assignment of the value without using `let`. This still works now if the script runs in the "compatibility mode", without `use strict`:
|
||||
|
||||
```js run no-strict
|
||||
// note: no "use strict" in this example
|
||||
|
||||
num = 5; // the variable "num" is created if it didn't exist
|
||||
|
||||
alert(num); // 5
|
||||
```
|
||||
|
||||
This is a bad practice and would cause an error in strict mode.
|
||||
````
|
||||
|
||||
### Variable naming
|
||||
|
||||
There are two limitations on variable names in JavaScript:
|
||||
|
||||
1. The name must contain only letters, digits, or the symbols `$` and `_`.
|
||||
2. The first character must not be a digit.
|
||||
|
||||
Examples of valid names:
|
||||
|
||||
```js
|
||||
let userName;
|
||||
let test123;
|
||||
```
|
||||
|
||||
When the name contains multiple words, [camelCase](https://en.wikipedia.org/wiki/CamelCase) is commonly used. That is: words go one after another, each word after the first one starts with a capital letter: `myVeryLongName`.
|
||||
|
||||
Examples of incorrect variable names:
|
||||
|
||||
```js no-beautify
|
||||
let 1a; // cannot start with a digit
|
||||
|
||||
let my-name; // hyphens '-' aren't allowed in the name
|
||||
```
|
||||
|
||||
```smart header="Case matters"
|
||||
Variables named `apple` and `AppLE` are two different variables.
|
||||
```
|
||||
|
||||
````smart header="Non-Latin letters are allowed, but not recommended"
|
||||
It is possible to use any language, including cyrillic letters or even hieroglyphs, like this:
|
||||
|
||||
```js
|
||||
let имя = "...";
|
||||
let 我 = "...";
|
||||
```
|
||||
|
||||
Technically, there is no error here. Such names are allowed, but there is an international convention to use English in variable names. Even if you're writing a small script, it may have a long life ahead. People from other countries may need to read it in the future.
|
||||
````
|
||||
|
||||
````warn header="Reserved names"
|
||||
There is a [list of reserved words](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords), which cannot be used as variable names because they are used by the language itself.
|
||||
|
||||
For example: `let`, `class`, `return`, and `function` are reserved.
|
||||
|
||||
The code below gives a syntax error:
|
||||
|
||||
```js run no-beautify
|
||||
let let = 5; // can't name a variable "let", error!
|
||||
let return = 5; // also can't name it "return", error!
|
||||
```
|
||||
````
|
||||
|
||||
### Other ways to declare a variable
|
||||
|
||||
Besides `let`, there are two other keywords that declare a variable:
|
||||
|
||||
- `var` (e.g. `var message`) -- the outdated way to declare a variable, you can meet it in really old scripts.
|
||||
Please don't use it.
|
||||
- `const` (e.g. `const message`) -- declares a *constant* variable.
|
||||
|
||||
A constant variable must be declared with the initial value, and afterwards it can't be reassigned.
|
||||
|
||||
For example:
|
||||
```js run
|
||||
const birthday = "18.04.1982";
|
||||
|
||||
birthday = "01.01.1970"; // Error: Assignment to constant variable.
|
||||
```
|
||||
|
||||
A person might change their name, but not the birthday. The idea of `const` is to let everyone (including the JavaScript engine)¸know about it.
|
||||
|
||||
## Statements and semicolons
|
||||
|
||||
The third line of our code is:
|
||||
|
||||
```js
|
||||
alert(message);
|
||||
```
|
||||
|
||||
|
||||
<hr>
|
||||
|
||||
|
||||
|
||||
It means that the script should run in the "strict mode".
|
||||
|
||||
Historically,
|
||||
|
||||
If we omit it, then some language features will work a little bit differently. We'll mention the differences later as
|
||||
|
||||
|
||||
or, in other words, in the modern mode of execution.
|
||||
|
||||
There are basically two modes of script execution of a script:
|
||||
- The "strict mode", .
|
||||
- The "strict mode".
|
||||
|
||||
What does it mean?
|
||||
|
||||
Well,
|
||||
|
||||
|
||||
There was a time long ago when JavaScript was a bit different language.
|
||||
|
||||
The core elements of scripts are statements.
|
||||
|
||||
Statements are syntax constructs and commands that perform actions, make JavaScript "do" something.
|
||||
|
||||
Here's an example of a statement:
|
||||
|
||||
```js run
|
||||
alert('Hello, world!');
|
||||
```
|
||||
|
||||
Click the "run" icon ▷ in the right-upper corner to see how it works.
|
||||
|
||||
We can have as many statements in our code as we want.
|
||||
|
||||
Statements can be separated with a semicolon.
|
||||
|
||||
For example, here we split "Hello World" into two alerts:
|
||||
|
||||
```js run no-beautify
|
||||
alert('Hello'); alert('World');
|
||||
```
|
||||
|
||||
Usually, statements are written on separate lines to make the code more readable:
|
||||
|
||||
```js run no-beautify
|
||||
alert('Hello');
|
||||
alert('World');
|
||||
```
|
||||
|
||||
A semicolon may be omitted in most cases when a line break exists.
|
||||
|
||||
This would also work:
|
||||
|
||||
```js run no-beautify
|
||||
alert('Hello')
|
||||
alert('World')
|
||||
```
|
||||
|
||||
Here, JavaScript interprets the line break as an "implicit" semicolon. This is called an [automatic semicolon insertion](https://tc39.github.io/ecma262/#sec-automatic-semicolon-insertion).
|
||||
|
||||
**In most cases, a newline implies a semicolon. But "in most cases" does not mean "always"!**
|
||||
|
||||
There are cases when a newline does not mean a semicolon.
|
||||
|
||||
For example:
|
||||
|
||||
```js run no-beautify
|
||||
alert(1 +
|
||||
2);
|
||||
```
|
||||
|
||||
The code outputs `3` because JavaScript does not insert semicolons after the plus `+`. It is intuitively obvious that if the line ends with a plus `"+"`, then it is an incomplete expression, that's continued on the next line. And in this case that works as intended.
|
||||
|
||||
**But there are situations where JavaScript "fails" to assume a semicolon where it is really needed.**
|
||||
|
||||
**TODO: The section below is optional, so it's collapsed by default - make it a hint on first open?**
|
||||
|
||||
**TODO: design this.**
|
||||
|
||||
````spoiler header="Read more about it"
|
||||
|
||||
If you're curious to see a concrete example of such an error, check this code out:
|
||||
|
||||
```js run
|
||||
[1, 2].forEach(alert)
|
||||
```
|
||||
|
||||
If the code is too complex to understand, that's all right. You don't have to.
|
||||
|
||||
All you need now is to run the code and remember the result: it shows `1` then `2`.
|
||||
|
||||
Let's add an `alert` before the code and *not* finish it with a semicolon:
|
||||
|
||||
```js run no-beautify
|
||||
alert("Without a semicolon after me - error")
|
||||
|
||||
[1, 2].forEach(alert)
|
||||
```
|
||||
|
||||
Now if you run the code, only the first `alert` is shown and then there's an error!
|
||||
|
||||
...But everything becomes fine again we add a semicolon after the `alert`:
|
||||
```js run
|
||||
alert("With a semicolon after me - all ok");
|
||||
|
||||
[1, 2].forEach(alert)
|
||||
```
|
||||
|
||||
The error in the no-semicolon variant occurs because JavaScript doesn't "auto-insert" a semicolon on a newline before square brackets `[...]`.
|
||||
|
||||
So, because the semicolon is not auto-inserted, the code in the first example is treated as a single statement. Here's how the engine sees it:
|
||||
|
||||
```js run no-beautify
|
||||
alert("There will be an error")[1, 2].forEach(alert)
|
||||
```
|
||||
|
||||
But such a merge in this case is just wrong, hence the error. This can happen in other situations as well.
|
||||
````
|
||||
|
||||
|
||||
We recommend putting semicolons between statements even if they are separated by newlines. This rule is widely adopted by the community. Let's note once again -- *it is possible* to leave out semicolons most of the time. But it's safer --
|
||||
especially for a beginner -- to use them.
|
||||
|
||||
## Comments
|
||||
|
||||
As time goes on, programs become more and more complex. It becomes necessary to add *comments* which describe what the code does and why.
|
||||
|
||||
Comments can be put into any place of a script. They don't affect its execution because the engine simply ignores them.
|
||||
|
||||
**One-line comments start with two forward slash characters `//`.**
|
||||
|
||||
The rest of the line is a comment. It may occupy a full line of its own or follow a statement.
|
||||
|
||||
Like here:
|
||||
```js run
|
||||
// This comment occupies a line of its own
|
||||
alert('Hello');
|
||||
|
||||
alert('World'); // This comment follows the statement
|
||||
```
|
||||
|
||||
**Multiline comments start with a forward slash and an asterisk <code>/*</code> and end with an asterisk and a forward slash <code>*/</code>.**
|
||||
|
||||
Like this:
|
||||
|
||||
```js run
|
||||
/* An example with two messages.
|
||||
This is a multiline comment.
|
||||
*/
|
||||
alert('Hello');
|
||||
alert('World');
|
||||
```
|
||||
|
||||
The content of comments is ignored, so if we put code inside <code>/* ... */</code>, it won't execute.
|
||||
|
||||
Sometimes it can be handy to temporarily disable a part of code:
|
||||
|
||||
```js run
|
||||
/* Commenting out the code
|
||||
alert('Hello');
|
||||
*/
|
||||
alert('World');
|
||||
```
|
||||
|
||||
```smart header="Use hotkeys!"
|
||||
In most editors, a line of code can be commented out by pressing the `key:Ctrl+/` hotkey for a single-line comment and something like `key:Ctrl+Shift+/` -- for multiline comments (select a piece of code and press the hotkey). For Mac, try `key:Cmd` instead of `key:Ctrl` and `key:Option` instead of `key:Shift`.
|
||||
```
|
||||
|
||||
````warn header="Nested comments are not supported!"
|
||||
There may not be `/*...*/` inside another `/*...*/`.
|
||||
|
||||
Such code will die with an error:
|
||||
|
||||
```js run no-beautify
|
||||
/*
|
||||
/* nested comment ?!? */
|
||||
*/
|
||||
alert( 'World' );
|
||||
```
|
||||
````
|
||||
|
||||
Please, don't hesitate to comment your code.
|
||||
|
||||
Comments increase the overall code footprint, but that's not a problem at all. There are many tools which minify code before publishing to a production server. They remove comments, so they don't appear in the working scripts. Therefore, comments do not have negative effects on production at all.
|
||||
|
||||
Later in the tutorial there will be a chapter <info:code-quality> that also explains how to write better comments.
|
1
1-js/01-getting-started/02-basics/use-strict.svg
Normal file
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" width="196" height="129" viewBox="0 0 196 129"><defs><style>@import url(https://fonts.googleapis.com/css?family=Open+Sans:bold,italic,bolditalic%7CPT+Mono);@font-face{font-family:'PT Mono';font-weight:700;font-style:normal;src:local('PT MonoBold'),url(/font/PTMonoBold.woff2) format('woff2'),url(/font/PTMonoBold.woff) format('woff'),url(/font/PTMonoBold.ttf) format('truetype')}</style></defs><g id="v2" fill="none" fill-rule="evenodd" stroke="none" stroke-width="1"><g id="use-strict.svg"><g id="noun_Cloud_3562756" fill="#000" fill-rule="nonzero" transform="translate(36 14)"><path id="Path" d="M32.236 42.259a38.415 38.415 0 01-8.566.951C10.596 43.208.003 36.98 0 29.297c.003-7.684 10.596-13.91 23.67-13.91 3.684 0 7.168.501 10.272 1.386C35.919 7.32 49.589.002 66.207 0c15.85.002 29.019 6.662 31.92 15.48.875-.06 1.766-.094 2.672-.094V17.383a32.79 32.79 0 00-3.75.217c-.461.051-.93-.008-1.309-.17-.384-.158-.635-.4-.698-.673C93.07 8.42 80.927 1.997 66.205 2 50.505 1.998 37.738 9.3 37.116 18.44c-.027.355-.372.68-.912.844a2.77 2.77 0 01-1.699-.041c-3.147-1.173-6.847-1.86-10.838-1.86-11.194.01-20.248 5.333-20.268 11.913.02 6.583 9.074 11.903 20.271 11.916 3.294 0 6.383-.472 9.14-1.29a2.763 2.763 0 011.59.016c.491.16.817.45.87.78.837 5.206 8.3 9.287 17.383 9.287 6.236 0 11.69-1.93 14.788-4.826.247-.23.64-.391 1.09-.444a2.692 2.692 0 011.3.145c2.042.806 4.485 1.276 7.131 1.276 6.202.004 11.367-2.6 12.533-6.04.107-.306.45-.568.924-.704a2.804 2.804 0 011.508.004c2.684.792 5.682 1.245 8.875 1.245 10.932-.012 19.779-5.211 19.8-11.638-.021-6.43-8.868-11.63-19.803-11.642V15.385c12.812 0 23.198 6.105 23.201 13.638-.003 7.531-10.389 13.633-23.201 13.635-3.01 0-5.883-.346-8.515-.96-2.168 3.755-8.206 6.453-15.324 6.455-2.734 0-5.318-.401-7.579-1.106-3.798 3-9.872 4.953-16.728 4.953-10.055-.001-18.438-4.177-20.417-9.741z"/></g><text id=""use-strict"" fill="#8A704D" font-family="PTMono-Regular, PT Mono" font-size="14" font-weight="normal"><tspan x="45.1" y="47">"use strict"</tspan></text><text id="...modern-code..." fill="#8A704D" font-family="PTMono-Regular, PT Mono" font-size="14" font-weight="normal"><tspan x="24.6" y="94">...modern code...</tspan></text></g></g></svg>
|
After Width: | Height: | Size: 2.2 KiB |
1
1-js/01-getting-started/02-basics/variable-change.svg
Normal file
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" width="392" height="192" viewBox="0 0 392 192"><defs><style>@import url(https://fonts.googleapis.com/css?family=Open+Sans:bold,italic,bolditalic%7CPT+Mono);@font-face{font-family:'PT Mono';font-weight:700;font-style:normal;src:local('PT MonoBold'),url(/font/PTMonoBold.woff2) format('woff2'),url(/font/PTMonoBold.woff) format('woff'),url(/font/PTMonoBold.ttf) format('truetype')}</style></defs><g id="v2" fill="none" fill-rule="evenodd" stroke="none" stroke-width="1"><g id="variable-change.svg"><g id="noun_1211_cc" transform="translate(52 -5)"><path id="Shape" fill="#E8C48F" d="M25 94.196h112.558v42.95c0 .373-.079.862-.279 1.294-.2.433-16.574 35.56-16.574 35.56v-54.36l16.854-25.444L156 66H43.44L25 94.196zM25 123V95l-15 28"/><g id="Rectangle-5-+-"World!"" transform="translate(0 57)"><path id="Rectangle-5" fill="#FFF9EB" stroke="#8A704D" stroke-width="2" d="M18.861 1.809L2 17.533l53.14 56.986L72 58.794 18.861 1.81z"/><text id=""World!"" fill="#8A704D" font-family="OpenSans-Bold, Open Sans" font-size="14" font-weight="bold" transform="rotate(47 40.083 39.762)"><tspan x="10.591" y="46.262">"World!"</tspan></text></g><g id="Rectangle-5-+-"World!"-2" transform="rotate(-67 96.824 -33.912)"><path id="Rectangle-5" fill="#FFF9EB" stroke="#8A704D" stroke-width="2" d="M18.861 1.809L2 17.533l53.14 56.986L72 58.794 18.861 1.81z"/><text id=""Hello!"" fill="#8A704D" font-family="OpenSans-Bold, Open Sans" font-size="14" font-weight="bold" transform="rotate(47 40.083 39.762)"><tspan x="10.591" y="46.262">"Hello!"</tspan></text></g><path id="Shape" fill="#E8C48F" d="M8 125v54.73c0 3.42 1.484 5.27 4.387 5.27h100.086c3.122 0 5.527-2.548 5.527-3.476V125H8z"/></g><text id="message" fill="#FFF" font-family="OpenSans-Bold, Open Sans" font-size="18" font-weight="bold"><tspan x="77" y="157">message</tspan></text><path id="Fill-54" fill="#E8C48F" d="M58.112 51.808S47.657 40.623 40.719 36.155l-.505 5.542a76.036 76.036 0 00-33.769 4.595l4.169 11.032a64.248 64.248 0 0128.531-3.882l-.505 5.542c5.581-3.329 19.472-7.176 19.472-7.176" transform="rotate(11 32.278 47.57)"/><path id="Fill-54" fill="#E8C48F" d="M287.797 28.186s-10.454-11.185-17.393-15.653l-.505 5.541a76.036 76.036 0 00-33.769 4.596l4.169 11.032a64.248 64.248 0 0128.531-3.882l-.504 5.541c5.58-3.328 19.47-7.175 19.47-7.175" transform="rotate(2 261.964 23.947)"/><g id="noun_48910_cc" transform="translate(298 5)"><path id="Shape" d="M50.983 6H36.016C35.456 6 35 6.626 35 7.395V12h17V7.395C52 6.626 51.543 6 50.983 6z"/><path id="Shape" fill="#E8C48F" d="M84.193 9.36h-26.39V6.085C57.803 2.729 54.99 0 51.528 0H36.47c-3.46 0-6.275 2.729-6.275 6.085V9.36H3.807C1.705 9.36 0 11.012 0 13.05v.26C0 15.348 1.705 17 3.807 17h80.386C86.295 17 88 15.348 88 13.31v-.26c0-2.038-1.706-3.69-3.807-3.69zM53 12H36V7.395C36 6.626 36.457 6 37.016 6h14.968C52.544 6 53 6.626 53 7.395V12zM74.955 20.045H8.044c-3.89 0-7.044-.68-7.044 3.266l5.282 78.382c0 3.943 3.155 7.307 7.045 7.307h56.347c3.89 0 7.044-3.364 7.044-7.307L82 23.31c-.001-3.947-3.155-3.266-7.045-3.266zM26.757 98.999c-1.283.039-2.353-.8-2.396-1.878l-2.36-61.095c-.041-1.078.964-1.985 2.242-2.025 1.283-.04 2.353.801 2.396 1.879l2.36 61.096c.041 1.076-.963 1.984-2.242 2.023zM43 97.049C43 98.126 42.328 99 41.5 99s-1.5-.876-1.5-1.951V35.95c0-1.078.672-1.951 1.5-1.951s1.5.873 1.5 1.951V97.05zm18.639.072c-.042 1.078-1.113 1.917-2.396 1.878-1.28-.04-2.283-.947-2.242-2.024l2.36-61.095c.042-1.078 1.112-1.919 2.394-1.879 1.28.042 2.285.947 2.244 2.025l-2.36 61.095z"/></g></g></g></svg>
|
After Width: | Height: | Size: 3.6 KiB |
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" width="359" height="143" viewBox="0 0 359 143"><defs><style>@import url(https://fonts.googleapis.com/css?family=Open+Sans:bold,italic,bolditalic%7CPT+Mono);@font-face{font-family:'PT Mono';font-weight:700;font-style:normal;src:local('PT MonoBold'),url(/font/PTMonoBold.woff2) format('woff2'),url(/font/PTMonoBold.woff) format('woff'),url(/font/PTMonoBold.ttf) format('truetype')}</style></defs><g id="v2" fill="none" fill-rule="evenodd" stroke="none" stroke-width="1"><g id="variable-copy-value.svg"><g id="noun_1211_cc-+-Message" transform="translate(11 6)"><g id="noun_1211_cc"><path id="Shape" fill="#E8C48F" d="M17 37.196h112.558v42.95c0 .373-.079.862-.279 1.294-.2.433-16.574 35.56-16.574 35.56V62.64l16.854-25.444L148 9H35.44L17 37.196zM17 66V38L2 66"/><g id="Rectangle-5-+-"World!"" transform="translate(15)"><path id="Rectangle-5" fill="#FFF9EB" stroke="#8A704D" stroke-width="2" d="M18.861 1.809L2 17.533l53.14 56.986L72 58.794 18.861 1.81z"/><text id=""Hello!"" fill="#8A704D" font-family="OpenSans-Bold, Open Sans" font-size="14" font-weight="bold" transform="rotate(47 38.202 38.946)"><tspan x="2.822" y="43.482">"Hello!"</tspan></text></g><path id="Shape" fill="#E8C48F" d="M0 68v54.73c0 3.42 1.484 5.27 4.387 5.27h100.086c3.122 0 5.527-2.548 5.527-3.476V68H0z"/></g><text id="message" fill="#FFF" font-family="OpenSans-Bold, Open Sans" font-size="18" font-weight="bold"><tspan x="17" y="105">message</tspan></text></g><g id="Group" transform="translate(210 6)"><path id="Shape" fill="#E8C48F" d="M17 37.196h113.417v42.95c0 .373-.08.862-.28 1.294-.202.433-16.702 35.56-16.702 35.56V62.64l16.983-25.444L149 9H35.582L17 37.196z"/><path id="Shape" fill="#E8C48F" d="M18 66V38L2 66"/><g id="Rectangle-5-+-"World!"" transform="translate(15)"><path id="Rectangle-5" fill="#FFF9EB" stroke="#8A704D" stroke-width="2" d="M19.117 1.8l-17.1 15.734 53.866 56.994 17.1-15.734L19.118 1.799z"/><text id=""Hello!"" fill="#8A704D" font-family="OpenSans-Bold, Open Sans" font-size="14" font-weight="bold" transform="rotate(47 38.162 37.693)"><tspan x="2.782" y="42.23">"Hello!"</tspan></text></g><path id="Shape" fill="#E8C48F" d="M0 68v54.73c0 3.42 1.497 5.27 4.427 5.27h100.996c3.15 0 5.577-2.548 5.577-3.476V68H0z"/><text id="hello" fill="#FFF" font-family="OpenSans-Bold, Open Sans" font-size="18" font-weight="bold"><tspan x="25" y="105">hello</tspan></text></g></g></g></svg>
|
After Width: | Height: | Size: 2.5 KiB |
1
1-js/01-getting-started/02-basics/variable.svg
Normal file
|
@ -0,0 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" width="166" height="145" viewBox="0 0 166 145"><defs><style>@import url(https://fonts.googleapis.com/css?family=Open+Sans:bold,italic,bolditalic%7CPT+Mono);@font-face{font-family:'PT Mono';font-weight:700;font-style:normal;src:local('PT MonoBold'),url(/font/PTMonoBold.woff2) format('woff2'),url(/font/PTMonoBold.woff) format('woff'),url(/font/PTMonoBold.ttf) format('truetype')}</style></defs><g id="v2" fill="none" fill-rule="evenodd" stroke="none" stroke-width="1"><g id="variable.svg"><g id="noun_1211_cc-+-Message" transform="translate(13 3)"><g id="noun_1211_cc"><path id="Shape" fill="#E8C48F" d="M17 37.196h112.558v42.95c0 .373-.079.862-.279 1.294-.2.433-16.574 35.56-16.574 35.56V62.64l16.854-25.444L148 9H35.44L17 37.196zM17 66V38L2 66"/><g id="Rectangle-5-+-"World!"" transform="translate(15)"><path id="Rectangle-5" fill="#FFF9EB" stroke="#8A704D" stroke-width="2" d="M18.861 1.809L2 17.533l53.14 56.986L72 58.794 18.861 1.81z"/><text id=""Hello!"" fill="#8A704D" font-family="OpenSans-Bold, Open Sans" font-size="14" font-weight="bold" transform="rotate(47 40.083 39.762)"><tspan x="10.591" y="46.262">"Hello!"</tspan></text></g><path id="Shape" fill="#E8C48F" d="M0 68v54.73c0 3.42 1.484 5.27 4.387 5.27h100.086c3.122 0 5.527-2.548 5.527-3.476V68H0z"/></g><text id="message" fill="#FFF" font-family="OpenSans-Bold, Open Sans" font-size="18" font-weight="bold"><tspan x="17" y="105">message</tspan></text></g></g></g></svg>
|
After Width: | Height: | Size: 1.5 KiB |
|
@ -1,121 +0,0 @@
|
|||
# An Introduction to JavaScript
|
||||
|
||||
Let's see what's so special about JavaScript, what we can achieve with it, and which other technologies play well with it.
|
||||
|
||||
## What is JavaScript?
|
||||
|
||||
*JavaScript* was initially created to "make web pages alive".
|
||||
|
||||
The programs in this language are called *scripts*. They can be written right in a web page's HTML and run automatically as the page loads.
|
||||
|
||||
Scripts are provided and executed as plain text. They don't need special preparation or compilation to run.
|
||||
|
||||
In this aspect, JavaScript is very different from another language called [Java](https://en.wikipedia.org/wiki/Java_(programming_language)).
|
||||
|
||||
```smart header="Why is it called <u>Java</u>Script?"
|
||||
When JavaScript was created, it initially had another name: "LiveScript". But Java was very popular at that time, so it was decided that positioning a new language as a "younger brother" of Java would help.
|
||||
|
||||
But as it evolved, JavaScript became a fully independent language with its own specification called [ECMAScript](http://en.wikipedia.org/wiki/ECMAScript), and now it has no relation to Java at all.
|
||||
```
|
||||
|
||||
Today, JavaScript can execute not only in the browser, but also on the server, or actually on any device that has a special program called [the JavaScript engine](https://en.wikipedia.org/wiki/JavaScript_engine).
|
||||
|
||||
The browser has an embedded engine sometimes called a "JavaScript virtual machine".
|
||||
|
||||
Different engines have different "codenames". For example:
|
||||
|
||||
- [V8](https://en.wikipedia.org/wiki/V8_(JavaScript_engine)) -- in Chrome and Opera.
|
||||
- [SpiderMonkey](https://en.wikipedia.org/wiki/SpiderMonkey) -- in Firefox.
|
||||
- ...There are other codenames like "Chakra" for IE, "ChakraCore" for Microsoft Edge, "Nitro" and "SquirrelFish" for Safari, etc.
|
||||
|
||||
The terms above are good to remember because they are used in developer articles on the internet. We'll use them too. For instance, if "a feature X is supported by V8", then it probably works in Chrome and Opera.
|
||||
|
||||
```smart header="How do engines work?"
|
||||
|
||||
Engines are complicated. But the basics are easy.
|
||||
|
||||
1. The engine (embedded if it's a browser) reads ("parses") the script.
|
||||
2. Then it converts ("compiles") the script to the machine language.
|
||||
3. And then the machine code runs, pretty fast.
|
||||
|
||||
The engine applies optimizations at each step of the process. It even watches the compiled script as it runs, analyzes the data that flows through it, and further optimizes the machine code based on that knowledge.
|
||||
```
|
||||
|
||||
## What can in-browser JavaScript do?
|
||||
|
||||
Modern JavaScript is a "safe" programming language. It does not provide low-level access to memory or CPU, because it was initially created for browsers which do not require it.
|
||||
|
||||
JavaScript's capabilities greatly depend on the environment it's running in. For instance, [Node.js](https://wikipedia.org/wiki/Node.js) supports functions that allow JavaScript to read/write arbitrary files, perform network requests, etc.
|
||||
|
||||
In-browser JavaScript can do everything related to webpage manipulation, interaction with the user, and the webserver.
|
||||
|
||||
For instance, in-browser JavaScript is able to:
|
||||
|
||||
- Add new HTML to the page, change the existing content, modify styles.
|
||||
- React to user actions, run on mouse clicks, pointer movements, key presses.
|
||||
- Send requests over the network to remote servers, download and upload files (so-called [AJAX](https://en.wikipedia.org/wiki/Ajax_(programming)) and [COMET](https://en.wikipedia.org/wiki/Comet_(programming)) technologies).
|
||||
- Get and set cookies, ask questions to the visitor, show messages.
|
||||
- Remember the data on the client-side ("local storage").
|
||||
|
||||
## What CAN'T in-browser JavaScript do?
|
||||
|
||||
JavaScript's abilities in the browser are limited for the sake of the user's safety. The aim is to prevent an evil webpage from accessing private information or harming the user's data.
|
||||
|
||||
Examples of such restrictions include:
|
||||
|
||||
- JavaScript on a webpage may not read/write arbitrary files on the hard disk, copy them or execute programs. It has no direct access to OS functions.
|
||||
|
||||
Modern browsers allow it to work with files, but the access is limited and only provided if the user does certain actions, like "dropping" a file into a browser window or selecting it via an `<input>` tag.
|
||||
|
||||
There are ways to interact with camera/microphone and other devices, but they require a user's explicit permission. So a JavaScript-enabled page may not sneakily enable a web-camera, observe the surroundings and send the information to the [NSA](https://en.wikipedia.org/wiki/National_Security_Agency).
|
||||
- Different tabs/windows generally do not know about each other. Sometimes they do, for example when one window uses JavaScript to open the other one. But even in this case, JavaScript from one page may not access the other if they come from different sites (from a different domain, protocol or port).
|
||||
|
||||
This is called the "Same Origin Policy". To work around that, *both pages* must agree for data exchange and contain a special JavaScript code that handles it. We'll cover that in the tutorial.
|
||||
|
||||
This limitation is, again, for the user's safety. A page from `http://anysite.com` which a user has opened must not be able to access another browser tab with the URL `http://gmail.com` and steal information from there.
|
||||
- JavaScript can easily communicate over the net to the server where the current page came from. But its ability to receive data from other sites/domains is crippled. Though possible, it requires explicit agreement (expressed in HTTP headers) from the remote side. Once again, that's a safety limitation.
|
||||
|
||||

|
||||
|
||||
Such limits do not exist if JavaScript is used outside of the browser, for example on a server. Modern browsers also allow plugin/extensions which may ask for extended permissions.
|
||||
|
||||
## What makes JavaScript unique?
|
||||
|
||||
There are at least *three* great things about JavaScript:
|
||||
|
||||
```compare
|
||||
+ Full integration with HTML/CSS.
|
||||
+ Simple things are done simply.
|
||||
+ Support by all major browsers and enabled by default.
|
||||
```
|
||||
JavaScript is the only browser technology that combines these three things.
|
||||
|
||||
That's what makes JavaScript unique. That's why it's the most widespread tool for creating browser interfaces.
|
||||
|
||||
That said, JavaScript also allows to create servers, mobile applications, etc.
|
||||
|
||||
## Languages "over" JavaScript
|
||||
|
||||
The syntax of JavaScript does not suit everyone's needs. Different people want different features.
|
||||
|
||||
That's to be expected, because projects and requirements are different for everyone.
|
||||
|
||||
So recently a plethora of new languages appeared, which are *transpiled* (converted) to JavaScript before they run in the browser.
|
||||
|
||||
Modern tools make the transpilation very fast and transparent, actually allowing developers to code in another language and auto-converting it "under the hood".
|
||||
|
||||
Examples of such languages:
|
||||
|
||||
- [CoffeeScript](http://coffeescript.org/) is a "syntactic sugar" for JavaScript. It introduces shorter syntax, allowing us to write clearer and more precise code. Usually, Ruby devs like it.
|
||||
- [TypeScript](http://www.typescriptlang.org/) is concentrated on adding "strict data typing" to simplify the development and support of complex systems. It is developed by Microsoft.
|
||||
- [Flow](http://flow.org/) also adds data typing, but in a different way. Developed by Facebook.
|
||||
- [Dart](https://www.dartlang.org/) is a standalone language that has its own engine that runs in non-browser environments (like mobile apps), but also can be transpiled to JavaScript. Developed by Google.
|
||||
- [Brython](https://brython.info/) is a Python transpiler to JavaScript that allow to write application in pure Python without JavaScript.
|
||||
|
||||
There are more. Of course, even if we use one of transpiled languages, we should also know JavaScript to really understand what we're doing.
|
||||
|
||||
## Summary
|
||||
|
||||
- JavaScript was initially created as a browser-only language, but is now used in many other environments as well.
|
||||
- Today, JavaScript has a unique position as the most widely-adopted browser language with full integration with HTML/CSS.
|
||||
- There are many languages that get "transpiled" to JavaScript and provide certain features. It is recommended to take a look at them, at least briefly, after mastering JavaScript.
|
Before Width: | Height: | Size: 14 KiB |
|
@ -1,42 +0,0 @@
|
|||
|
||||
# Manuals and specifications
|
||||
|
||||
This book is a *tutorial*. It aims to help you gradually learn the language. But once you're familiar with the basics, you'll need other sources.
|
||||
|
||||
## Specification
|
||||
|
||||
[The ECMA-262 specification](https://www.ecma-international.org/publications/standards/Ecma-262.htm) contains the most in-depth, detailed and formalized information about JavaScript. It defines the language.
|
||||
|
||||
But being that formalized, it's difficult to understand at first. So if you need the most trustworthy source of information about the language details, the specification is the right place. But it's not for everyday use.
|
||||
|
||||
A new specification version is released every year. In-between these releases, the latest specification draft is at <https://tc39.es/ecma262/>.
|
||||
|
||||
To read about new bleeding-edge features, including those that are "almost standard" (so-called "stage 3"), see proposals at <https://github.com/tc39/proposals>.
|
||||
|
||||
Also, if you're in developing for the browser, then there are other specs covered in the [second part](info:browser-environment) of the tutorial.
|
||||
|
||||
## Manuals
|
||||
|
||||
- **MDN (Mozilla) JavaScript Reference** is a manual with examples and other information. It's great to get in-depth information about individual language functions, methods etc.
|
||||
|
||||
One can find it at <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference>.
|
||||
|
||||
Although, it's often best to use an internet search instead. Just use "MDN [term]" in the query, e.g. <https://google.com/search?q=MDN+parseInt> to search for `parseInt` function.
|
||||
|
||||
|
||||
- **MSDN** – Microsoft manual with a lot of information, including JavaScript (often referred to as JScript). If one needs something specific to Internet Explorer, better go there: <http://msdn.microsoft.com/>.
|
||||
|
||||
Also, we can use an internet search with phrases such as "RegExp MSDN" or "RegExp MSDN jscript".
|
||||
|
||||
## Compatibility tables
|
||||
|
||||
JavaScript is a developing language, new features get added regularly.
|
||||
|
||||
To see their support among browser-based and other engines, see:
|
||||
|
||||
- <http://caniuse.com> - per-feature tables of support, e.g. to see which engines support modern cryptography functions: <http://caniuse.com/#feat=cryptography>.
|
||||
- <https://kangax.github.io/compat-table> - a table with language features and engines that support those or don't support.
|
||||
|
||||
All these resources are useful in real-life development, as they contain valuable information about language details, their support etc.
|
||||
|
||||
Please remember them (or this page) for the cases when you need in-depth information about a particular feature.
|
|
@ -1,46 +0,0 @@
|
|||
# Code editors
|
||||
|
||||
A code editor is the place where programmers spend most of their time.
|
||||
|
||||
There are two main types of code editors: IDEs and lightweight editors. Many people use one tool of each type.
|
||||
|
||||
## IDE
|
||||
|
||||
The term [IDE](https://en.wikipedia.org/wiki/Integrated_development_environment) (Integrated Development Environment) refers to a powerful editor with many features that usually operates on a "whole project." As the name suggests, it's not just an editor, but a full-scale "development environment."
|
||||
|
||||
An IDE loads the project (which can be many files), allows navigation between files, provides autocompletion based on the whole project (not just the open file), and integrates with a version management system (like [git](https://git-scm.com/)), a testing environment, and other "project-level" stuff.
|
||||
|
||||
If you haven't selected an IDE yet, consider the following options:
|
||||
|
||||
- [Visual Studio Code](https://code.visualstudio.com/) (cross-platform, free).
|
||||
- [WebStorm](http://www.jetbrains.com/webstorm/) (cross-platform, paid).
|
||||
|
||||
For Windows, there's also "Visual Studio", not to be confused with "Visual Studio Code". "Visual Studio" is a paid and mighty Windows-only editor, well-suited for the .NET platform. It's also good at JavaScript. There's also a free version [Visual Studio Community](https://www.visualstudio.com/vs/community/).
|
||||
|
||||
Many IDEs are paid, but have a trial period. Their cost is usually negligible compared to a qualified developer's salary, so just choose the best one for you.
|
||||
|
||||
## Lightweight editors
|
||||
|
||||
"Lightweight editors" are not as powerful as IDEs, but they're fast, elegant and simple.
|
||||
|
||||
They are mainly used to open and edit a file instantly.
|
||||
|
||||
The main difference between a "lightweight editor" and an "IDE" is that an IDE works on a project-level, so it loads much more data on start, analyzes the project structure if needed and so on. A lightweight editor is much faster if we need only one file.
|
||||
|
||||
In practice, lightweight editors may have a lot of plugins including directory-level syntax analyzers and autocompleters, so there's no strict border between a lightweight editor and an IDE.
|
||||
|
||||
The following options deserve your attention:
|
||||
|
||||
- [Atom](https://atom.io/) (cross-platform, free).
|
||||
- [Visual Studio Code](https://code.visualstudio.com/) (cross-platform, free).
|
||||
- [Sublime Text](http://www.sublimetext.com) (cross-platform, shareware).
|
||||
- [Notepad++](https://notepad-plus-plus.org/) (Windows, free).
|
||||
- [Vim](http://www.vim.org/) and [Emacs](https://www.gnu.org/software/emacs/) are also cool if you know how to use them.
|
||||
|
||||
## Let's not argue
|
||||
|
||||
The editors in the lists above are those that either I or my friends whom I consider good developers have been using for a long time and are happy with.
|
||||
|
||||
There are other great editors in our big world. Please choose the one you like the most.
|
||||
|
||||
The choice of an editor, like any other tool, is individual and depends on your projects, habits, and personal preferences.
|
|
@ -1,63 +0,0 @@
|
|||
# Developer console
|
||||
|
||||
Code is prone to errors. You will quite likely make errors... Oh, what am I talking about? You are *absolutely* going to make errors, at least if you're a human, not a [robot](https://en.wikipedia.org/wiki/Bender_(Futurama)).
|
||||
|
||||
But in the browser, users don't see errors by default. So, if something goes wrong in the script, we won't see what's broken and can't fix it.
|
||||
|
||||
To see errors and get a lot of other useful information about scripts, "developer tools" have been embedded in browsers.
|
||||
|
||||
Most developers lean towards Chrome or Firefox for development because those browsers have the best developer tools. Other browsers also provide developer tools, sometimes with special features, but are usually playing "catch-up" to Chrome or Firefox. So most developers have a "favorite" browser and switch to others if a problem is browser-specific.
|
||||
|
||||
Developer tools are potent; they have many features. To start, we'll learn how to open them, look at errors, and run JavaScript commands.
|
||||
|
||||
## Google Chrome
|
||||
|
||||
Open the page [bug.html](bug.html).
|
||||
|
||||
There's an error in the JavaScript code on it. It's hidden from a regular visitor's eyes, so let's open developer tools to see it.
|
||||
|
||||
Press `key:F12` or, if you're on Mac, then `key:Cmd+Opt+J`.
|
||||
|
||||
The developer tools will open on the Console tab by default.
|
||||
|
||||
It looks somewhat like this:
|
||||
|
||||

|
||||
|
||||
The exact look of developer tools depends on your version of Chrome. It changes from time to time but should be similar.
|
||||
|
||||
- Here we can see the red-colored error message. In this case, the script contains an unknown "lalala" command.
|
||||
- On the right, there is a clickable link to the source `bug.html:12` with the line number where the error has occurred.
|
||||
|
||||
Below the error message, there is a blue `>` symbol. It marks a "command line" where we can type JavaScript commands. Press `key:Enter` to run them.
|
||||
|
||||
Now we can see errors, and that's enough for a start. We'll come back to developer tools later and cover debugging more in-depth in the chapter <info:debugging-chrome>.
|
||||
|
||||
```smart header="Multi-line input"
|
||||
Usually, when we put a line of code into the console, and then press `key:Enter`, it executes.
|
||||
|
||||
To insert multiple lines, press `key:Shift+Enter`. This way one can enter long fragments of JavaScript code.
|
||||
```
|
||||
|
||||
## Firefox, Edge, and others
|
||||
|
||||
Most other browsers use `key:F12` to open developer tools.
|
||||
|
||||
The look & feel of them is quite similar. Once you know how to use one of these tools (you can start with Chrome), you can easily switch to another.
|
||||
|
||||
## Safari
|
||||
|
||||
Safari (Mac browser, not supported by Windows/Linux) is a little bit special here. We need to enable the "Develop menu" first.
|
||||
|
||||
Open Preferences and go to the "Advanced" pane. There's a checkbox at the bottom:
|
||||
|
||||

|
||||
|
||||
Now `key:Cmd+Opt+C` can toggle the console. Also, note that the new top menu item named "Develop" has appeared. It has many commands and options.
|
||||
|
||||
## Summary
|
||||
|
||||
- Developer tools allow us to see errors, run commands, examine variables, and much more.
|
||||
- They can be opened with `key:F12` for most browsers on Windows. Chrome for Mac needs `key:Cmd+Opt+J`, Safari: `key:Cmd+Opt+C` (need to enable first).
|
||||
|
||||
Now we have the environment ready. In the next section, we'll get down to JavaScript.
|
|
@ -1,17 +0,0 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
There is an error in the script on this page.
|
||||
<script>
|
||||
lalala
|
||||
</script>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
Before Width: | Height: | Size: 41 KiB |
Before Width: | Height: | Size: 68 KiB |
Before Width: | Height: | Size: 107 KiB |
Before Width: | Height: | Size: 266 KiB |
|
@ -88,7 +88,7 @@ In older scripts, you may also find another keyword: `var` instead of `let`:
|
|||
```
|
||||
|
||||
The `var` keyword is *almost* the same as `let`. It also declares a variable, but in a slightly different, "old-school" way.
|
||||
|
||||
|
||||
There are subtle differences between `let` and `var`, but they do not matter for us yet. We'll cover them in detail in the chapter <info:var>.
|
||||
````
|
||||
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" width="392" height="192" viewBox="0 0 392 192"><defs><style>@import url(https://fonts.googleapis.com/css?family=Open+Sans:bold,italic,bolditalic%7CPT+Mono);@font-face{font-family:'PT Mono';font-weight:700;font-style:normal;src:local('PT MonoBold'),url(/font/PTMonoBold.woff2) format('woff2'),url(/font/PTMonoBold.woff) format('woff'),url(/font/PTMonoBold.ttf) format('truetype')}</style></defs><g id="combined" fill="none" fill-rule="evenodd" stroke="none" stroke-width="1"><g id="variable-change.svg"><g id="noun_1211_cc" transform="translate(52 -5)"><path id="Shape" fill="#E8C48F" d="M25 94.196h112.558v42.95c0 .373-.079.862-.279 1.294-.2.433-16.574 35.56-16.574 35.56v-54.36l16.854-25.444L156 66H43.44L25 94.196zM25 123V95l-15 28"/><g id="Rectangle-5-+-"World!"" transform="translate(0 57)"><path id="Rectangle-5" fill="#FFF9EB" stroke="#8A704D" stroke-width="2" d="M18.861 1.809L2 17.533l53.14 56.986L72 58.794 18.861 1.81z"/><text id=""World!"" fill="#8A704D" font-family="OpenSans-Bold, Open Sans" font-size="14" font-weight="bold" transform="rotate(47 40.083 39.762)"><tspan x="10.591" y="46.262">"World!"</tspan></text></g><g id="Rectangle-5-+-"World!"-2" transform="rotate(-67 96.824 -33.912)"><path id="Rectangle-5" fill="#FFF9EB" stroke="#8A704D" stroke-width="2" d="M18.861 1.809L2 17.533l53.14 56.986L72 58.794 18.861 1.81z"/><text id=""Hello!"" fill="#8A704D" font-family="OpenSans-Bold, Open Sans" font-size="14" font-weight="bold" transform="rotate(47 40.083 39.762)"><tspan x="10.591" y="46.262">"Hello!"</tspan></text></g><path id="Shape" fill="#E8C48F" d="M8 125v54.73c0 3.42 1.484 5.27 4.387 5.27h100.086c3.122 0 5.527-2.548 5.527-3.476V125H8z"/></g><text id="message" fill="#FFF" font-family="OpenSans-Bold, Open Sans" font-size="18" font-weight="bold"><tspan x="77" y="157">message</tspan></text><path id="Fill-54" fill="#E8C48F" d="M58.112 51.808S47.657 40.623 40.719 36.155l-.505 5.542a76.036 76.036 0 00-33.769 4.595l4.169 11.032a64.248 64.248 0 0128.531-3.882l-.505 5.542c5.581-3.329 19.472-7.176 19.472-7.176" transform="rotate(11 32.278 47.57)"/><path id="Fill-54" fill="#E8C48F" d="M287.797 28.186s-10.454-11.185-17.393-15.653l-.505 5.541a76.036 76.036 0 00-33.769 4.596l4.169 11.032a64.248 64.248 0 0128.531-3.882l-.504 5.541c5.58-3.328 19.47-7.175 19.47-7.175" transform="rotate(2 261.964 23.947)"/><g id="noun_48910_cc" transform="translate(298 5)"><path id="Shape" d="M50.983 6H36.016C35.456 6 35 6.626 35 7.395V12h17V7.395C52 6.626 51.543 6 50.983 6z"/><path id="Shape" fill="#E8C48F" d="M84.193 9.36h-26.39V6.085C57.803 2.729 54.99 0 51.528 0H36.47c-3.46 0-6.275 2.729-6.275 6.085V9.36H3.807C1.705 9.36 0 11.012 0 13.05v.26C0 15.348 1.705 17 3.807 17h80.386C86.295 17 88 15.348 88 13.31v-.26c0-2.038-1.706-3.69-3.807-3.69zM53 12H36V7.395C36 6.626 36.457 6 37.016 6h14.968C52.544 6 53 6.626 53 7.395V12zM74.955 20.045H8.044c-3.89 0-7.044-.68-7.044 3.266l5.282 78.382c0 3.943 3.155 7.307 7.045 7.307h56.347c3.89 0 7.044-3.364 7.044-7.307L82 23.31c-.001-3.947-3.155-3.266-7.045-3.266zM26.757 98.999c-1.283.039-2.353-.8-2.396-1.878l-2.36-61.095c-.041-1.078.964-1.985 2.242-2.025 1.283-.04 2.353.801 2.396 1.879l2.36 61.096c.041 1.076-.963 1.984-2.242 2.023zM43 97.049C43 98.126 42.328 99 41.5 99s-1.5-.876-1.5-1.951V35.95c0-1.078.672-1.951 1.5-1.951s1.5.873 1.5 1.951V97.05zm18.639.072c-.042 1.078-1.113 1.917-2.396 1.878-1.28-.04-2.283-.947-2.242-2.024l2.36-61.095c.042-1.078 1.112-1.919 2.394-1.879 1.28.042 2.285.947 2.244 2.025l-2.36 61.095z"/></g></g></g></svg>
|
Before Width: | Height: | Size: 3.6 KiB |
|
@ -1 +0,0 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" width="166" height="145" viewBox="0 0 166 145"><defs><style>@import url(https://fonts.googleapis.com/css?family=Open+Sans:bold,italic,bolditalic%7CPT+Mono);@font-face{font-family:'PT Mono';font-weight:700;font-style:normal;src:local('PT MonoBold'),url(/font/PTMonoBold.woff2) format('woff2'),url(/font/PTMonoBold.woff) format('woff'),url(/font/PTMonoBold.ttf) format('truetype')}</style></defs><g id="combined" fill="none" fill-rule="evenodd" stroke="none" stroke-width="1"><g id="variable.svg"><g id="noun_1211_cc-+-Message" transform="translate(13 3)"><g id="noun_1211_cc"><path id="Shape" fill="#E8C48F" d="M17 37.196h112.558v42.95c0 .373-.079.862-.279 1.294-.2.433-16.574 35.56-16.574 35.56V62.64l16.854-25.444L148 9H35.44L17 37.196zM17 66V38L2 66"/><g id="Rectangle-5-+-"World!"" transform="translate(15)"><path id="Rectangle-5" fill="#FFF9EB" stroke="#8A704D" stroke-width="2" d="M18.861 1.809L2 17.533l53.14 56.986L72 58.794 18.861 1.81z"/><text id=""Hello!"" fill="#8A704D" font-family="OpenSans-Bold, Open Sans" font-size="14" font-weight="bold" transform="rotate(47 40.083 39.762)"><tspan x="10.591" y="46.262">"Hello!"</tspan></text></g><path id="Shape" fill="#E8C48F" d="M0 68v54.73c0 3.42 1.484 5.27 4.387 5.27h100.086c3.122 0 5.527-2.548 5.527-3.476V68H0z"/></g><text id="message" fill="#FFF" font-family="OpenSans-Bold, Open Sans" font-size="18" font-weight="bold"><tspan x="17" y="105">message</tspan></text></g></g></g></svg>
|
Before Width: | Height: | Size: 1.5 KiB |
|
@ -1 +1 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" width="359" height="143" viewBox="0 0 359 143"><defs><style>@import url(https://fonts.googleapis.com/css?family=Open+Sans:bold,italic,bolditalic%7CPT+Mono);@font-face{font-family:'PT Mono';font-weight:700;font-style:normal;src:local('PT MonoBold'),url(/font/PTMonoBold.woff2) format('woff2'),url(/font/PTMonoBold.woff) format('woff'),url(/font/PTMonoBold.ttf) format('truetype')}</style></defs><g id="combined" fill="none" fill-rule="evenodd" stroke="none" stroke-width="1"><g id="variable-copy-value.svg"><g id="noun_1211_cc-+-Message" transform="translate(11 6)"><g id="noun_1211_cc"><path id="Shape" fill="#E8C48F" d="M17 37.196h112.558v42.95c0 .373-.079.862-.279 1.294-.2.433-16.574 35.56-16.574 35.56V62.64l16.854-25.444L148 9H35.44L17 37.196zM17 66V38L2 66"/><g id="Rectangle-5-+-"World!"" transform="translate(15)"><path id="Rectangle-5" fill="#FFF9EB" stroke="#8A704D" stroke-width="2" d="M18.861 1.809L2 17.533l53.14 56.986L72 58.794 18.861 1.81z"/><text id=""Hello!"" fill="#8A704D" font-family="OpenSans-Bold, Open Sans" font-size="14" font-weight="bold" transform="rotate(47 38.202 38.946)"><tspan x="2.822" y="43.482">"Hello!"</tspan></text></g><path id="Shape" fill="#E8C48F" d="M0 68v54.73c0 3.42 1.484 5.27 4.387 5.27h100.086c3.122 0 5.527-2.548 5.527-3.476V68H0z"/></g><text id="message" fill="#FFF" font-family="OpenSans-Bold, Open Sans" font-size="18" font-weight="bold"><tspan x="17" y="105">message</tspan></text></g><g id="Group" transform="translate(210 6)"><path id="Shape" fill="#E8C48F" d="M17 37.196h113.417v42.95c0 .373-.08.862-.28 1.294-.202.433-16.702 35.56-16.702 35.56V62.64l16.983-25.444L149 9H35.582L17 37.196z"/><path id="Shape" fill="#E8C48F" d="M18 66V38L2 66"/><g id="Rectangle-5-+-"World!"" transform="translate(15)"><path id="Rectangle-5" fill="#FFF9EB" stroke="#8A704D" stroke-width="2" d="M19.117 1.8l-17.1 15.734 53.866 56.994 17.1-15.734L19.118 1.799z"/><text id=""Hello!"" fill="#8A704D" font-family="OpenSans-Bold, Open Sans" font-size="14" font-weight="bold" transform="rotate(47 38.162 37.693)"><tspan x="2.782" y="42.23">"Hello!"</tspan></text></g><path id="Shape" fill="#E8C48F" d="M0 68v54.73c0 3.42 1.497 5.27 4.427 5.27h100.996c3.15 0 5.577-2.548 5.577-3.476V68H0z"/><text id="phrase" fill="#FFF" font-family="OpenSans-Bold, Open Sans" font-size="18" font-weight="bold"><tspan x="25" y="105">phrase</tspan></text></g></g></g></svg>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="359" height="143" viewBox="0 0 359 143"><defs><style>@import url(https://fonts.googleapis.com/css?family=Open+Sans:bold,italic,bolditalic%7CPT+Mono);@font-face{font-family:'PT Mono';font-weight:700;font-style:normal;src:local('PT MonoBold'),url(/font/PTMonoBold.woff2) format('woff2'),url(/font/PTMonoBold.woff) format('woff'),url(/font/PTMonoBold.ttf) format('truetype')}</style></defs><g id="v2" fill="none" fill-rule="evenodd" stroke="none" stroke-width="1"><g id="variable-copy-value.svg"><g id="noun_1211_cc-+-Message" transform="translate(11 6)"><g id="noun_1211_cc"><path id="Shape" fill="#E8C48F" d="M17 37.196h112.558v42.95c0 .373-.079.862-.279 1.294-.2.433-16.574 35.56-16.574 35.56V62.64l16.854-25.444L148 9H35.44L17 37.196zM17 66V38L2 66"/><g id="Rectangle-5-+-"World!"" transform="translate(15)"><path id="Rectangle-5" fill="#FFF9EB" stroke="#8A704D" stroke-width="2" d="M18.861 1.809L2 17.533l53.14 56.986L72 58.794 18.861 1.81z"/><text id=""Hello!"" fill="#8A704D" font-family="OpenSans-Bold, Open Sans" font-size="14" font-weight="bold" transform="rotate(47 38.202 38.946)"><tspan x="2.822" y="43.482">"Hello!"</tspan></text></g><path id="Shape" fill="#E8C48F" d="M0 68v54.73c0 3.42 1.484 5.27 4.387 5.27h100.086c3.122 0 5.527-2.548 5.527-3.476V68H0z"/></g><text id="message" fill="#FFF" font-family="OpenSans-Bold, Open Sans" font-size="18" font-weight="bold"><tspan x="17" y="105">message</tspan></text></g><g id="Group" transform="translate(210 6)"><path id="Shape" fill="#E8C48F" d="M17 37.196h113.417v42.95c0 .373-.08.862-.28 1.294-.202.433-16.702 35.56-16.702 35.56V62.64l16.983-25.444L149 9H35.582L17 37.196z"/><path id="Shape" fill="#E8C48F" d="M18 66V38L2 66"/><g id="Rectangle-5-+-"World!"" transform="translate(15)"><path id="Rectangle-5" fill="#FFF9EB" stroke="#8A704D" stroke-width="2" d="M19.117 1.8l-17.1 15.734 53.866 56.994 17.1-15.734L19.118 1.799z"/><text id=""Hello!"" fill="#8A704D" font-family="OpenSans-Bold, Open Sans" font-size="14" font-weight="bold" transform="rotate(47 38.162 37.693)"><tspan x="2.782" y="42.23">"Hello!"</tspan></text></g><path id="Shape" fill="#E8C48F" d="M0 68v54.73c0 3.42 1.497 5.27 4.427 5.27h100.996c3.15 0 5.577-2.548 5.577-3.476V68H0z"/><text id="hello" fill="#FFF" font-family="OpenSans-Bold, Open Sans" font-size="18" font-weight="bold"><tspan x="25" y="105">hello</tspan></text></g></g></g></svg>
|
Before Width: | Height: | Size: 2.5 KiB After Width: | Height: | Size: 2.5 KiB |