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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This chapter briefly recaps the features of JavaScript that we've learned by now, paying special attention to subtle moments.
Давайте коротко повторимо вивчений матеріал і пригадаємо ключові моменти.
##Code structure
##Структура коду
Statements are delimited with a semicolon:
Інструкції розділяються крапкою з комою:
```js run no-beautify
alert('Hello'); alert('World');
alert('Привіт'); alert('Світ');
```
Usually, a line-break is also treated as a delimiter, so that would also work:
Зазвичай, перенесення рядка також вважається за розділювач, тому такий варіант теж працюватиме:
```js run no-beautify
alert('Hello')
alert('World')
alert('Привіт')
alert('Світ')
```
That's called "automatic semicolon insertion".Sometimes it doesn't work, for instance:
Це називається "автоматичне вставлення крапки з комою".Іноді такий варіант може не спрацювати, наприклад:
```js run
alert("There will be an error after this message")
alert("Після цього повідомлення буде помилка")
[1, 2].forEach(alert)
```
Most codestyle guides agree that we should put a semicolon after each statement.
Більшість посібників по стилю коду рекомендують ставити крапку з комою після кожної інструкції.
Semicolons are not required after code blocks`{...}`and syntax constructs with them like loops:
Крапку з комою не потрібно ставити після блоків коду`{...}`та синтаксичних конструкцій з ними, наприклад, після циклів:
```js
function f() {
//no semicolon needed after function declaration
//після оголошення функції не обов'язково ставити крапку з комою
}
for(;;) {
//no semicolon needed after the loop
//після циклу також немає потреби ставити крапку з комою
}
```
...But even if we can put an "extra" semicolon somewhere, that's not an error. It will be ignored.
...Але навіть якщо ми поставимо "зайву" крапку з комою, помилки не буде. Її просто буде проігноровано.
More in: <info:structure>.
Детальніше: <info:structure>.
##Strictmode
##Суворий режим (strictmode)
To fully enable all features of modernJavaScript,we should start scripts with `"use strict"`.
Щоб задіяти усі можливості сучасної мовиJavaScript,нам потрібно починати скрипти із конструкції `"use strict"`.
```js
'use strict';
...
...// ваш код
```
The directive must be at the top of a script or at the beginning of a function body.
Цю директиву потрібно розміщувати в першому рядку скрипта або на початку тіла функції.
Without `"use strict"`,everything still works, but some features behave in the old-fashion, "compatible" way. We'd generally prefer the modern behavior.
Без `"use strict"`,код все ще буде працювати, однак деякі можливості працюватимуть в старому режимі, так званому "режимі сумісності". Ми рекомендуємо використовувати суворий ("сучасний") режим.
Some modern features of the language (like classes that we'll study in the future) enable strict mode implicitly.
Деякі сучасні функції мови (наприклад, класи, які ми будемо вивчати в майбутньому) неявно вмикають суворий режим.
More in: <info:strict-mode>.
Детальніше: <info:strict-mode>.
##Variables
##Змінні
Can be declared using:
Можна оголошувати наступним чином:
- `let`
- `const` (constant, can't be changed)
- `var` (old-style, will see later)
- `const` (константа, не можна змінювати)
- `var` (старий спосіб, ми переглянемо його пізніше)
A variable name can include:
-Letters and digits, but the first character may not be a digit.
-Characters`$`and `_` are normal, on par with letters.
-Non-Latin alphabets and hieroglyphs are also allowed, but commonly not used.
Ім'я змінної може включати:
-Літери і цифри; першим символом має бути лише літера, не цифра.
-Допускаються символи`$`та `_`, в парі з літерами.
-Не латинські символи, як кирилиця та ієрогліфи також допускаються, але вони не мають широкого вжитку.
Variables are dynamically typed. They can store any value:
Змінні динамічно типізовані, тобто вони змінюють свій тип в залежності від присвоєного значення. Можуть зберігати будь-які значення:
```js
let x = 5;
x = "John";
let x = 5; // присвоєно тип "число"
x = "Іван"; // тип змінився на "рядок"
```
There are 7 data types:
Існує 7 типів даних:
- `number` for both floating-point and integer numbers,
- `string` for strings,
- `boolean` for logical values: `true/false`,
- `null` --a type with a single value`null`,meaning "empty" or "does not exist",
- `undefined` --a type with a single value`undefined`,meaning "not assigned",
- `object` and`symbol` --for complex data structures and unique identifiers, we haven't learnt them yet.
-число (`number`) для цілих та десяткових чисел,
-рядок (`string`) для тексту/слів,
-булевий тип (`boolean`) для логічних значень: `true/false`,
- `null` --тип з єдиним значенням`null`,який означає "пусто" або "не існує",
- `undefined` --тип з єдиним значенням`undefined`,який означає "не присвоєно",
-об'єкт (`object`) та символ (`symbol`) --для складних структур даних та унікальних ідентифікаторів, ми їх ще не вивчили.
The `typeof`operator returns the type for a value, with two exceptions:
Оператор `typeof`повертає тип змінної, за винятком двох випадків:
```js
typeof null == "object" //error in the language
typeof function(){} == "function" //functions are treated specially
typeof null == "object" //помилка в мові
typeof function(){} == "function" //спеціально для функцій
```
More in: <info:variables>and <info:types>.
Детальніше: <info:variables>та <info:types>.
##Interaction
##Взаємодія
We're using a browser as a working environment, so basic UI functions will be:
Ми використовуємо браузер у ролі робочого середовища, тому для взаємодії з відвідувачами ми використовуємо функції:
:Ask a`question`, and return either what the visitor entered or `null` if they clicked "cancel".
:Задає питання (`question`), а потім повертає те, що ввів відвідувач, або `null`, якщо відвідувач натиснув кнопку "Скасувати".
[`confirm(question)`](mdn:api/Window/confirm)
:Ask a`question` and suggest to choose between Ok and Cancel. The choice is returned as `true/false`.
:Задає питання (`question`) і пропонує відвідувачу вибрати "ОК" або "Скасувати". Вибір повертається як `true/false`.
[`alert(message)`](mdn:api/Window/alert)
:Output a`message`.
:Виводить повідомлення (`message`).
All these functions are *modal*, they pause the code execution and prevent the visitor from interacting with the page until they answer.
Всі ці функції показують *модальне вікно*; вони зупиняють виконання скрипта і не дають користувачеві взаємодіяти зі сторінкою доки не буде надана відповідь.
For instance:
Наприклад:
```js run
let userName = prompt("Your name?", "Alice");
letisTeaWanted = confirm("Do you want some tea?");
let userName = prompt("Ваше ім'я?", "Настя");
letisBunWanted = confirm("Хочете булочку?");
alert( "Visitor: " + userName ); //Alice
alert( "Tea wanted: " +isTeaWanted ); // true
alert( "Відвідувач: " + userName ); //Настя
alert( "Хоче булочку: " +isBunWanted ); // true
```
More in: <info:alert-prompt-confirm>.
Детальніше: <info:alert-prompt-confirm>.
##Operators
##Оператори
JavaScriptsupports the following operators:
JavaScriptпідтримує такі оператори:
Arithmetical
:Regular: `* + - /`,also`%` for the remainder and`**`for power of a number.
Арифметичні
:Звичайні: `* + - /`,а також оператори`%`, для визначення остачі від ділення та`**`для піднесення до степеня.
The binary plus`+`concatenates strings. And if any of the operands is a string, the other one is converted to string too:
Бінарний плюс`+`об'єднує (конкатинує) рядки. А якщо одним із операндів буде рядок, то інший операнд також буде конвертовано в рядок:
```js run
alert( '1' + 2 ); // '12',string
alert( 1 + '2' ); // '12', string
alert( '1' + 2 ); // '12',рядок
alert(typeof (1 + '2' ) ); // string
```
Assignments
:There is a simple assignment: `a = b`and combined ones like `a *= 2`.
Оператори присвоєння
:Звичайне присвоєння: `a = b`і складені `a *= 2`.
Bitwise
:Bitwise operators work with 32-bit integers at the lowest, bit-level: see the [docs](mdn:/JavaScript/Reference/Operators/Bitwise_Operators) when they are needed.
Бітові операції
:Бітові оператори працюють з 32-бітними цілими числами на найнижчому, побітовому, рівні. Детальніше про їхнє використання можна прочитати на ресурсі [MDN](mdn:/JavaScript/Reference/Operators/Bitwise_Operators).
Conditional
:The only operator with three parameters: `cond ? resultA : resultB`.If `cond`is truthy, returns `resultA`,otherwise `resultB`.
Умовний оператор
:Єдиний оператор з трьома параметрами: `cond ? resultA : resultB`.Якщо `cond`вірно, повертається `resultA`,інакше – `resultB`.
Logical operators
:Logical AND `&&`and OR `||`perform short-circuit evaluation and then return the value where it stopped. Logical NOT `!`converts the operand to boolean type and returns the inverse value.
Логічні оператори
:Логічні І `&&`та АБО `||`використовують так звані "ледачі обчислення" і насамкінець повертають `true` або `false`. Логічне НЕ `!`конвертує операнд в логічний тип і повертає інвертоване значення.
Comparisons
:Equality check`==`for values of different types converts them to a number (except`null`and `undefined` that equal each other and nothing else),so these are equal:
Порівнювання
:Перевірка на рівність`==`для значень різних типів, конвертує їх в число (за винятком`null`і `undefined`, які рівні тільки один одному і нічому іншому),тому приклади нижче рівні:
```js run
alert( 0 == false ); // true
alert( 0 == '' ); // true
```
Other comparisons convert to a number as well.
Інші оператори порівнювання також конвертують значення в числовий тип.
The strict equality operator`===`doesn't do the conversion: different types always mean different values for it.
Оператор строгої рівності`===`не виконує конвертацію: різні типи для нього завжди означають різні значення.
Values `null`and `undefined`are special: they equal `==`each other and don't equal anything else.
Значення `null`та `undefined`особливі: вони рівні `==`лише самим собі і не рівні нічому іншому.
Greater/less comparisons compare strings character-by-character, other types are converted to a number.
Оператори порівнювання більше/менше порівнюють рядки посимвольно, інші типи конвертуються в число.
Other operators
:There are few others, like a comma operator.
Інші оператори
:Існують й інші оператори, такі як кома.
More in: <info:operators>, <info:comparison>, <info:logical-operators>.
That was a brief list ofJavaScript features. As of now we've studied only basics. Further in the tutorial you'll find more specials and advanced features of JavaScript.
Це був короткий перелік можливостейJavaScript. Цієї миті ми вивчили лише основи. Далі в посібнику ви знайдете більше особливостей і просунутих можливостей JavaScript.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.