From 3e71798a24fba9d009aa708fcbd8ed96ee1684fc Mon Sep 17 00:00:00 2001 From: Mykola Sopiha Date: Sun, 14 Nov 2021 22:29:00 +0200 Subject: [PATCH 01/14] nullish-coalescing-operator --- .../12-nullish-coalescing-operator/article.md | 126 +++++++++--------- 1 file changed, 63 insertions(+), 63 deletions(-) diff --git a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md index b84dff892..681e67939 100644 --- a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md +++ b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md @@ -1,94 +1,94 @@ -# Nullish coalescing operator '??' +# Оператор об’єднання з null '??' [recent browser="new"] -The nullish coalescing operator is written as two question marks `??`. +Оператор об’єднання з null записується як два знаки питання `??`. -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`. +В цій статті ми будемо говорити, що значення виразу "визначене", якщо воно відрізняється від `null` чи `undefined`. -The result of `a ?? b` is: -- if `a` is defined, then `a`, -- if `a` isn't defined, then `b`. +Результатом `a ?? b` є: +- якщо `a` визначене, то `a`, +- якщо `b` визначене, то `b`, -In other words, `??` returns the first argument if it's not `null/undefined`. Otherwise, the second one. +Іншими словами, `??` повертає перший аргумент, якщо він не `null/undefined`. Інакше, другий. -The nullish coalescing operator isn't anything completely new. It's just a nice syntax to get the first "defined" value of the two. +Оператор об’єднання з null не є абсолютно новим. Це просто хороший синтаксис, щоб отримати перше "визначене" значення з двох. -We can rewrite `result = a ?? b` using the operators that we already know, like this: +Ми можемо переписати `result = a ?? b` використовуючи оператори, які ми вже знаємо: ```js result = (a !== null && a !== undefined) ? a : b; ``` -Now it should be absolutely clear what `??` does. Let's see where it helps. +Тепер повинно бути абсолютно зрозуміло, що робить `??`. Подивімось, де це допомагає. -The common use case for `??` is to provide a default value for a potentially undefined variable. +Загальний випадок використання `??` полягає в тому, щоб забезпечити значення за замовчуванням для потенційно невизначеної змінної. -For example, here we show `user` if defined, otherwise `Anonymous`: +Наприклад, тут ми показуємо `user`, якщо він визначений, інакше `Анонімний`: ```js run let user; -alert(user ?? "Anonymous"); // Anonymous (user not defined) +alert(user ?? "Анонімний"); // Анонімний (user не визначений) ``` -Here's the example with `user` assigned to a name: +Ось приклад з `user`, якому присвоєне ім’я: ```js run -let user = "John"; +let user = "Іван"; -alert(user ?? "Anonymous"); // John (user defined) +alert(user ?? "Анонімний"); // Іван (user визначений) ``` -We can also use a sequence of `??` to select the first value from a list that isn't `null/undefined`. +Ми також можемо використовувати послідовність з `??`, щоб вибрати перше значення зі списку, яке не є `null/undefined`. -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. +Скажімо, у нас є дані користувача в змінних `firstName`,` lastName` або `nickName`. Всі вони можуть бути не визначені, якщо користувач вирішив не вводити значення. -We'd like to display the user name using one of these variables, or show "Anonymous" if all of them aren't defined. +Ми хотіли б показати ім’я користувача, використовуючи одну з цих змінних, або показати "Анонімний", якщо всі вони не визначені. -Let's use the `??` operator for that: +Використаймо оператор `??` для цього: ```js run let firstName = null; let lastName = null; -let nickName = "Supercoder"; +let nickName = "Суперкодер"; -// shows the first defined value: +// показує перше визначене значення: *!* -alert(firstName ?? lastName ?? nickName ?? "Anonymous"); // Supercoder +alert(firstName ?? lastName ?? nickName ?? "Анонімний"); // Суперкодер */!* ``` -## Comparison with || +## Порівняння з || -The OR `||` operator can be used in the same way as `??`, as it was described in the [previous chapter](info:logical-operators#or-finds-the-first-truthy-value). +Оператор АБО `||` може бути використаний таким же чином, як `??`, як це було описано в [попередній главі](info:logical-operators#or-finds-the-first-truthy-value). -For example, in the code above we could replace `??` with `||` and still get the same result: +Наприклад, у коді вище, ми могли б замінити `??` на `||` і все ще отримали б в той же результат: ```js run let firstName = null; let lastName = null; -let nickName = "Supercoder"; +let nickName = "Суперкодер"; -// shows the first truthy value: +// показує перше істинне значення: *!* -alert(firstName || lastName || nickName || "Anonymous"); // Supercoder +alert(firstName || lastName || nickName || "Анонімний"); // Суперкодер */!* ``` -Historically, the OR `||` operator was there first. It exists since the beginning of JavaScript, so developers were using it for such purposes for a long time. +Історично, оператор АБО `||` був першим. Він існує з початку JavaScript, тому розробники використовували його для цих цілей протягом тривалого часу. -On the other hand, the nullish coalescing operator `??` was added to JavaScript only recently, and the reason for that was that people weren't quite happy with `||`. +З іншого боку, оператор об’єднання з null `??` було нещодавно додано в JavaScript, і причиною того було те, що люди були не дуже задоволені `||`. -The important difference between them is that: -- `||` returns the first *truthy* value. -- `??` returns the first *defined* value. +Важлива різниця між ними полягає в тому, що: +- `||` повертає перше *істинне* значення. +- `??` повертає перше *визначене* значення. -In other words, `||` doesn't distinguish between `false`, `0`, an empty string `""` and `null/undefined`. They are all the same -- falsy values. If any of these is the first argument of `||`, then we'll get the second argument as the result. +Іншими словами, `||` не розрізняє `false`, `0`, порожній рядок `""` та `null/undefined`. Всі вони однакові -- хибні значення. Якщо будь-яке з них є першим аргументом `||`, тоді ми отримаємо другий аргумент як результат. -In practice though, we may want to use default value only when the variable is `null/undefined`. That is, when the value is really unknown/not set. +Однак на практиці, ми хочемо використовувати значення за замовчуванням лише тоді, коли змінна `null/undefined`. Тобто, коли значення дійсно невідоме/не встановлене. -For example, consider this: +Наприклад, розгляньте це: ```js run let height = 0; @@ -97,73 +97,73 @@ alert(height || 100); // 100 alert(height ?? 100); // 0 ``` -- The `height || 100` checks `height` for being a falsy value, and it's `0`, falsy indeed. - - so the result of `||` is the second argument, `100`. -- The `height ?? 100` checks `height` for being `null/undefined`, and it's not, - - so the result is `height` "as is", that is `0`. +- `height || 100` перевіряє чи має змінна `height` має хибне значення, і `0` -- це дійсно хибне значення. + - отже, результатом `||` є другий аргумент, `100`. +- `height ?? 100` перевіряє змінну `height`, чи вона `null/undefined`, і це не так, + - отже, результат -- `height` "як є", тобто `0`. -In practice, the zero height is often a valid value, that shouldn't be replaced with the default. So `??` does just the right thing. +На практиці нульова висота часто є дійсним значенням, яке не слід замінювати на значення за замовчуванням. Отже, `??` робить саме так, як треба. -## Precedence +## Пріоритет -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`. +Пріоритет оператора `??` приблизно такий же, як `||`, тільки трохи нижче. Він дорівнює `5` у [таблиці MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table), а пріоритет `||` -- `6`. -That means that, just like `||`, the nullish coalescing operator `??` is evaluated before `=` and `?`, but after most other operations, such as `+`, `*`. +Це означає, що, як і `||`, оператор об’єднання з null `??` оцінюється до `=` та `?`, але після більшості інших операцій, таких як `+`, `*`. -So if we'd like to choose a value with `??` in an expression with other operators, consider adding parentheses: +Отже, якщо ми хотіли б вибрати значення за допомогою `??` у виразі з іншими операторами, розгляньте додавання дужок: ```js run let height = null; let width = null; -// important: use parentheses +// важливо: використовуйте дужки let area = (height ?? 100) * (width ?? 50); alert(area); // 5000 ``` -Otherwise, if we omit parentheses, then as `*` has the higher precedence than `??`, it would execute first, leading to incorrect results. +В іншому випадку, якщо ми опускаємо дужки, то, оскільки `*` має вищий пріоритет, ніж `??`, то він буде виконуватися першим, що призводить до неправильних результатів. ```js -// without parentheses +// без дужок let area = height ?? 100 * width ?? 50; -// ...works the same as this (probably not what we want): +// ...працює так само, як попередній вираз (мабуть, це не те, що ми хочемо): let area = height ?? (100 * width) ?? 50; ``` -### Using ?? with && or || +### Використання ?? разом з && або || -Due to safety reasons, JavaScript forbids using `??` together with `&&` and `||` operators, unless the precedence is explicitly specified with parentheses. +З міркувань безпеки, JavaScript забороняє використання `??` разом з операторами `&&` та `||`, якщо пріоритет явно не вказаний дужками. -The code below triggers a syntax error: +Код нижче викликає синтаксичну помилку: ```js run -let x = 1 && 2 ?? 3; // Syntax error +let x = 1 && 2 ?? 3; // Синтаксична помилка ``` -The limitation is surely debatable, it was added to the language specification with the purpose to avoid programming mistakes, when people start to switch from `||` to `??`. +Обмеження є досить спірним, воно було додано до специфікації мови з метою уникнення помилок програмування, коли люди почнуть переходити з `||` до `??`. -Use explicit parentheses to work around it: +Використовуйте явні дужки, щоб працювати з цим оператором: ```js run *!* -let x = (1 && 2) ?? 3; // Works +let x = (1 && 2) ?? 3; // Працює */!* alert(x); // 2 ``` -## Summary +## Підсумки -- The nullish coalescing operator `??` provides a short way to choose the first "defined" value from a list. +- Оператор об’єднання з null `??` надає короткий спосіб вибору першого "визначеного" значення зі списку. - It's used to assign default values to variables: + Він використовується для присвоєння значень за замовчуванням до змінних: ```js - // set height=100, if height is null or undefined + // встановлює height=100, якщо height null чи undefined height = height ?? 100; ``` -- The operator `??` has a very low precedence, only a bit higher than `?` and `=`, so consider adding parentheses when using it in an expression. -- It's forbidden to use it with `||` or `&&` without explicit parentheses. +- Оператор `??` має дуже низький пріоритет, лише трохи вищий, ніж `?` та `=`, тому розглядайте додавання дужок при використанні його у виразах. +- Цей оператор заборонено використовувати з `||` або `&&` без явних дужок. From 51bcbb3fd21b3907d4ec09f8a4b811768c004ec2 Mon Sep 17 00:00:00 2001 From: Taras Date: Mon, 15 Nov 2021 18:02:16 +0200 Subject: [PATCH 02/14] Update 1-js/02-first-steps/12-nullish-coalescing-operator/article.md --- 1-js/02-first-steps/12-nullish-coalescing-operator/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md index 681e67939..4043e2cfb 100644 --- a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md +++ b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md @@ -4,7 +4,7 @@ Оператор об’єднання з null записується як два знаки питання `??`. -В цій статті ми будемо говорити, що значення виразу "визначене", якщо воно відрізняється від `null` чи `undefined`. +В цій статті ми будемо писати, що значення виразу "визначене", якщо воно відрізняється від `null` чи `undefined`. Результатом `a ?? b` є: - якщо `a` визначене, то `a`, From cf3e97c94acafd124ca82376c37318bf0f386de5 Mon Sep 17 00:00:00 2001 From: Mykola Sopiha Date: Mon, 15 Nov 2021 19:28:36 +0200 Subject: [PATCH 03/14] Update 1-js/02-first-steps/12-nullish-coalescing-operator/article.md Co-authored-by: Taras --- .../12-nullish-coalescing-operator/article.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md index 4043e2cfb..4e7c62b2f 100644 --- a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md +++ b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md @@ -6,9 +6,9 @@ В цій статті ми будемо писати, що значення виразу "визначене", якщо воно відрізняється від `null` чи `undefined`. -Результатом `a ?? b` є: -- якщо `a` визначене, то `a`, -- якщо `b` визначене, то `b`, +Результатом `a ?? b` буде: +- `a`, якщо `a` визначене, +- `b`, якщо `a` не визначене. Іншими словами, `??` повертає перший аргумент, якщо він не `null/undefined`. Інакше, другий. From cc9250a9ea9b962fc69b8d2e37dfcde90bbdb602 Mon Sep 17 00:00:00 2001 From: Mykola Sopiha Date: Mon, 15 Nov 2021 22:25:20 +0200 Subject: [PATCH 04/14] Update 1-js/02-first-steps/12-nullish-coalescing-operator/article.md Co-authored-by: Taras --- 1-js/02-first-steps/12-nullish-coalescing-operator/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md index 4e7c62b2f..b23d4ea3d 100644 --- a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md +++ b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md @@ -10,7 +10,7 @@ - `a`, якщо `a` визначене, - `b`, якщо `a` не визначене. -Іншими словами, `??` повертає перший аргумент, якщо він не `null/undefined`. Інакше, другий. +Інакше кажучи, `??` повертає перший аргумент, якщо він не `null/undefined`. Інакше, другий. Оператор об’єднання з null не є абсолютно новим. Це просто хороший синтаксис, щоб отримати перше "визначене" значення з двох. From 0f1c6f4a8b23190e1b9c425267e61fd2be50b418 Mon Sep 17 00:00:00 2001 From: Mykola Sopiha Date: Mon, 15 Nov 2021 22:25:31 +0200 Subject: [PATCH 05/14] Update 1-js/02-first-steps/12-nullish-coalescing-operator/article.md Co-authored-by: Taras --- 1-js/02-first-steps/12-nullish-coalescing-operator/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md index b23d4ea3d..c9d39ff4d 100644 --- a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md +++ b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md @@ -14,7 +14,7 @@ Оператор об’єднання з null не є абсолютно новим. Це просто хороший синтаксис, щоб отримати перше "визначене" значення з двох. -Ми можемо переписати `result = a ?? b` використовуючи оператори, які ми вже знаємо: +Ми можемо переписати вираз `result = a ?? b`, використовуючи оператори, які ми вже знаємо: ```js result = (a !== null && a !== undefined) ? a : b; From 5dac8e7283fe032604a4c76fdfb77d448808447a Mon Sep 17 00:00:00 2001 From: Mykola Sopiha Date: Mon, 15 Nov 2021 22:25:43 +0200 Subject: [PATCH 06/14] Update 1-js/02-first-steps/12-nullish-coalescing-operator/article.md Co-authored-by: Taras --- 1-js/02-first-steps/12-nullish-coalescing-operator/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md index c9d39ff4d..0453e312c 100644 --- a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md +++ b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md @@ -22,7 +22,7 @@ result = (a !== null && a !== undefined) ? a : b; Тепер повинно бути абсолютно зрозуміло, що робить `??`. Подивімось, де це допомагає. -Загальний випадок використання `??` полягає в тому, щоб забезпечити значення за замовчуванням для потенційно невизначеної змінної. +В загальному, оператор `??` використовують, щоб забезпечити типове значення для потенційно невизначеної змінної. Наприклад, тут ми показуємо `user`, якщо він визначений, інакше `Анонімний`: From aa57e17e8dc34daafe82aa3c41e9a8081023f69e Mon Sep 17 00:00:00 2001 From: Mykola Sopiha Date: Mon, 15 Nov 2021 22:25:54 +0200 Subject: [PATCH 07/14] Update 1-js/02-first-steps/12-nullish-coalescing-operator/article.md Co-authored-by: Taras --- 1-js/02-first-steps/12-nullish-coalescing-operator/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md index 0453e312c..2b6d70a35 100644 --- a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md +++ b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md @@ -42,7 +42,7 @@ alert(user ?? "Анонімний"); // Іван (user визначений) Ми також можемо використовувати послідовність з `??`, щоб вибрати перше значення зі списку, яке не є `null/undefined`. -Скажімо, у нас є дані користувача в змінних `firstName`,` lastName` або `nickName`. Всі вони можуть бути не визначені, якщо користувач вирішив не вводити значення. +Скажімо, у нас є дані користувача в змінних `firstName`, `lastName` або `nickName`. Всі вони можуть бути не визначені, якщо користувач вирішив не вводити значення. Ми хотіли б показати ім’я користувача, використовуючи одну з цих змінних, або показати "Анонімний", якщо всі вони не визначені. From ea797b3d4a6cf60acdb5f537486448c40a83e656 Mon Sep 17 00:00:00 2001 From: Mykola Sopiha Date: Mon, 15 Nov 2021 22:26:26 +0200 Subject: [PATCH 08/14] Update 1-js/02-first-steps/12-nullish-coalescing-operator/article.md Co-authored-by: Taras --- 1-js/02-first-steps/12-nullish-coalescing-operator/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md index 2b6d70a35..4ccd1d505 100644 --- a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md +++ b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md @@ -106,7 +106,7 @@ alert(height ?? 100); // 0 ## Пріоритет -Пріоритет оператора `??` приблизно такий же, як `||`, тільки трохи нижче. Він дорівнює `5` у [таблиці MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table), а пріоритет `||` -- `6`. +Пріоритет оператора `??` приблизно такий самий, як `||`, тільки трохи нижчий. Він дорівнює `5` у [таблиці MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table), а пріоритет `||` -- `6`. Це означає, що, як і `||`, оператор об’єднання з null `??` оцінюється до `=` та `?`, але після більшості інших операцій, таких як `+`, `*`. From d6dcd3b24045fcbb2f34007ce87b68d505d93b2e Mon Sep 17 00:00:00 2001 From: Mykola Sopiha Date: Mon, 15 Nov 2021 22:26:38 +0200 Subject: [PATCH 09/14] Update 1-js/02-first-steps/12-nullish-coalescing-operator/article.md Co-authored-by: Taras --- 1-js/02-first-steps/12-nullish-coalescing-operator/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md index 4ccd1d505..4bed443fd 100644 --- a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md +++ b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md @@ -165,5 +165,5 @@ alert(x); // 2 height = height ?? 100; ``` -- Оператор `??` має дуже низький пріоритет, лише трохи вищий, ніж `?` та `=`, тому розглядайте додавання дужок при використанні його у виразах. +- Оператор `??` має дуже низький пріоритет -- трохи вищий, ніж `?` та `=`, тому розглядайте додавання дужок при використанні його у виразах. - Цей оператор заборонено використовувати з `||` або `&&` без явних дужок. From 7aed53e4e60af10ebe3f618b797c2e3b2f4a4cc9 Mon Sep 17 00:00:00 2001 From: Mykola Sopiha Date: Mon, 15 Nov 2021 22:26:51 +0200 Subject: [PATCH 10/14] Update 1-js/02-first-steps/12-nullish-coalescing-operator/article.md Co-authored-by: Taras --- 1-js/02-first-steps/12-nullish-coalescing-operator/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md index 4bed443fd..5fac32969 100644 --- a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md +++ b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md @@ -63,7 +63,7 @@ alert(firstName ?? lastName ?? nickName ?? "Анонімний"); // Супер Оператор АБО `||` може бути використаний таким же чином, як `??`, як це було описано в [попередній главі](info:logical-operators#or-finds-the-first-truthy-value). -Наприклад, у коді вище, ми могли б замінити `??` на `||` і все ще отримали б в той же результат: +Наприклад, у коді вище, ми могли б замінити `??` на `||` і все ще отримали б той самий результат: ```js run let firstName = null; From a6ea8f4d4172b6012b4c9628592cdb7ba1f40052 Mon Sep 17 00:00:00 2001 From: Mykola Sopiha Date: Mon, 15 Nov 2021 22:27:16 +0200 Subject: [PATCH 11/14] Update 1-js/02-first-steps/12-nullish-coalescing-operator/article.md Co-authored-by: Taras --- 1-js/02-first-steps/12-nullish-coalescing-operator/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md index 5fac32969..50397b24f 100644 --- a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md +++ b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md @@ -84,7 +84,7 @@ alert(firstName || lastName || nickName || "Анонімний"); // Супер - `||` повертає перше *істинне* значення. - `??` повертає перше *визначене* значення. -Іншими словами, `||` не розрізняє `false`, `0`, порожній рядок `""` та `null/undefined`. Всі вони однакові -- хибні значення. Якщо будь-яке з них є першим аргументом `||`, тоді ми отримаємо другий аргумент як результат. +Інакше кажучи, оператор `||` не розрізняє, чи значення `false`, `0`, порожній рядок `""` чи `null/undefined`. Всі вони однакові -- хибні значення. Якщо будь-яке з них є першим аргументом `||`, тоді ми отримаємо другий аргумент як результат. Однак на практиці, ми хочемо використовувати значення за замовчуванням лише тоді, коли змінна `null/undefined`. Тобто, коли значення дійсно невідоме/не встановлене. From 267578f43bdda5a7ab3f1f3267e5a4e81b8fb559 Mon Sep 17 00:00:00 2001 From: Mykola Sopiha Date: Fri, 19 Nov 2021 19:19:02 +0200 Subject: [PATCH 12/14] Update 1-js/02-first-steps/12-nullish-coalescing-operator/article.md Co-authored-by: Taras --- 1-js/02-first-steps/12-nullish-coalescing-operator/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md index 50397b24f..9196c7b64 100644 --- a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md +++ b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md @@ -86,7 +86,7 @@ alert(firstName || lastName || nickName || "Анонімний"); // Супер Інакше кажучи, оператор `||` не розрізняє, чи значення `false`, `0`, порожній рядок `""` чи `null/undefined`. Всі вони однакові -- хибні значення. Якщо будь-яке з них є першим аргументом `||`, тоді ми отримаємо другий аргумент як результат. -Однак на практиці, ми хочемо використовувати значення за замовчуванням лише тоді, коли змінна `null/undefined`. Тобто, коли значення дійсно невідоме/не встановлене. +Однак на практиці, ми хочемо використовувати типове значення лише тоді, коли змінна `null/undefined`. Тобто, коли значення дійсно невідоме/не встановлене. Наприклад, розгляньте це: From 4863012ed809c4ce28264e75a14bfa6d9df6ee42 Mon Sep 17 00:00:00 2001 From: Mykola Sopiha Date: Fri, 19 Nov 2021 19:19:07 +0200 Subject: [PATCH 13/14] Update 1-js/02-first-steps/12-nullish-coalescing-operator/article.md Co-authored-by: Taras --- 1-js/02-first-steps/12-nullish-coalescing-operator/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md index 9196c7b64..443806bd5 100644 --- a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md +++ b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md @@ -158,7 +158,7 @@ alert(x); // 2 - Оператор об’єднання з null `??` надає короткий спосіб вибору першого "визначеного" значення зі списку. - Він використовується для присвоєння значень за замовчуванням до змінних: + Він використовується для присвоєння типових значень до змінних: ```js // встановлює height=100, якщо height null чи undefined From d2a21e727e350519f8473dcbb9012a43cf88c873 Mon Sep 17 00:00:00 2001 From: Mykola Sopiha Date: Fri, 19 Nov 2021 19:19:14 +0200 Subject: [PATCH 14/14] Update 1-js/02-first-steps/12-nullish-coalescing-operator/article.md Co-authored-by: Taras --- 1-js/02-first-steps/12-nullish-coalescing-operator/article.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md index 443806bd5..4fc415081 100644 --- a/1-js/02-first-steps/12-nullish-coalescing-operator/article.md +++ b/1-js/02-first-steps/12-nullish-coalescing-operator/article.md @@ -102,7 +102,7 @@ alert(height ?? 100); // 0 - `height ?? 100` перевіряє змінну `height`, чи вона `null/undefined`, і це не так, - отже, результат -- `height` "як є", тобто `0`. -На практиці нульова висота часто є дійсним значенням, яке не слід замінювати на значення за замовчуванням. Отже, `??` робить саме так, як треба. +На практиці нульова висота часто є дійсним значенням, яке не слід замінювати на типове значення. Отже, `??` робить саме те, що треба. ## Пріоритет pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy