Telegram Group & Telegram Channel
🧠 Задача по JavaScript: Асинхронность и замыкания

Что выведет следующий код?


for (var i = 0; i < 3; i++) {
setTimeout(function () {
console.log(i);
}, 100);
}


Варианты ответа:
A)

1
2

B)

3
3

C)

1
2
(в случайном порядке)

D)



---

Правильный ответ: B

Почему:
Переменная
i объявлена через var, а значит — она общая для всех итераций. К моменту выполнения setTimeout, цикл уже завершился, и значение и равно 3. Каждая отложенная функция видит одно и то же значение.

@javascriptv



tg-me.com/javascriptv/1816
Create:
Last Update:

🧠 Задача по JavaScript: Асинхронность и замыкания

Что выведет следующий код?


for (var i = 0; i < 3; i++) {
setTimeout(function () {
console.log(i);
}, 100);
}


Варианты ответа:
A)

1
2

B)

3
3

C)

1
2
(в случайном порядке)

D)



---

Правильный ответ: B

Почему:
Переменная
i объявлена через var, а значит — она общая для всех итераций. К моменту выполнения setTimeout, цикл уже завершился, и значение и равно 3. Каждая отложенная функция видит одно и то же значение.

@javascriptv

BY Javascript


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/javascriptv/1816

View MORE
Open in Telegram


Javascript Telegram | DID YOU KNOW?

Date: |

Find Channels On Telegram?

Telegram is an aspiring new messaging app that’s taking the world by storm. The app is free, fast, and claims to be one of the safest messengers around. It allows people to connect easily, without any boundaries.You can use channels on Telegram, which are similar to Facebook pages. If you’re wondering how to find channels on Telegram, you’re in the right place. Keep reading and you’ll find out how. Also, you’ll learn more about channels, creating channels yourself, and the difference between private and public Telegram channels.

At a time when the Indian stock market is peaking and has rallied immensely compared to global markets, there are companies that have not performed in the last 10 years. These are definitely a minor portion of the market considering there are hundreds of stocks that have turned multibagger since 2020. What went wrong with these stocks? Reasons vary from corporate governance, sectoral weakness, company specific and so on. But the more important question is, are these stocks worth buying?

Javascript from it


Telegram Javascript
FROM USA