Files
freeCodeCamp/curriculum/challenges/ukrainian/02-javascript-algorithms-and-data-structures/basic-javascript/local-scope-and-functions.md
2022-10-25 16:43:05 -05:00

87 lines
2.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
id: 56533eb9ac21ba0edf2244bf
title: Локальна область видимості та функції
challengeType: 1
videoUrl: 'https://scrimba.com/c/cd62NhM'
forumTopicId: 18227
dashedName: local-scope-and-functions
---
# --description--
Змінні, оголошені в межах функції, мають <dfn>локальну</dfn> область дії (як і параметри функції). Це означає, що вони є видимими лише в межах цієї функції.
Це функція `myTest` з локальною змінною `loc`.
```js
function myTest() {
const loc = "foo";
console.log(loc);
}
myTest();
console.log(loc);
```
Виклик функції `myTest()` відображатиме рядок `foo` в консолі. Рядок `console.log(loc)` (за межами функції `myTest`) видасть помилку, тому що `loc` не визначено поза функцією.
# --instructions--
Редактор має два `console.log` щоб допомогти вам побачити, що відбувається. Перевіряйте консоль, коли програмуєте, щоб побачити зміни. Оголосіть локальну змінну `myVar` всередині `myLocalScope` та запустіть тести.
**Примітка:** на консолі досі буде `ReferenceError: myVar is not defined`, але це не спричинить збій тестів.
# --hints--
Код не повинен містити глобальну змінну `myVar`.
```js
function declared() {
myVar;
}
assert.throws(declared, ReferenceError);
```
Вам слід додати локальну змінну `myVar`.
```js
assert(
/functionmyLocalScope\(\)\{.*(var|let|const)myVar[\s\S]*}/.test(
__helpers.removeWhiteSpace(code)
)
);
```
# --seed--
## --seed-contents--
```js
function myLocalScope() {
// Only change code below this line
console.log('inside myLocalScope', myVar);
}
myLocalScope();
// Run and check the console
// myVar is not defined outside of myLocalScope
console.log('outside myLocalScope', myVar);
```
# --solutions--
```js
function myLocalScope() {
// Only change code below this line
let myVar;
console.log('inside myLocalScope', myVar);
}
myLocalScope();
// Run and check the console
// myVar is not defined outside of myLocalScope
console.log('outside myLocalScope', myVar);
```