試験JavaScript-Developer-I-JPN トピック1 問題83 スレッド

Salesforce JavaScript-Developer-I-JPNのリアル試験問題集
問題 #: 83
トピック #: 1
以下のコードを参照してください。
01 let total = 10;
02 const interval = setInterval(() = > {
03 total++;
04 clearInterval(interval);
05 total++;
06 }, 0);
07 total++;
08 console.log(total);
JavaScriptはシングルスレッドであることを考慮すると、コード実行後の8行目の出力はどうなりますか?

おすすめの解答:A 解答を投票する

* Synchronous execution order JavaScript executes code in a single thread, following a well-defined order:
* All synchronous code runs first, line by line.
* Asynchronous callbacks (like those scheduled with setInterval or setTimeout) are placed into the event queue and executed only after the current call stack is empty.
Let's follow the code step by step:
* Line 01:
* let total = 10;
total is initialized with the value 10.
* Line 02:
* const interval = setInterval(() = > {
* total++;
* clearInterval(interval);
* total++;
* }, 0);
setInterval schedules the callback function to run repeatedly after a delay of at least 0 milliseconds, but it does not run immediately. The callback is added to the timer queue and will be invoked after the current synchronous script finishes and the event loop gets to process timer callbacks.
At this point, interval holds the interval ID, but the callback has not executed yet.
* Line 07:
* total++;
This is still synchronous, so it runs before any scheduled callbacks.
total was 10, now it becomes 11.
* Line 08:
* console.log(total);
At this moment, the interval callback has still not run (because the event loop has not yet processed the timer queue).
So total is 11, and console.log(total); outputs 11.
Therefore, the value printed at line 08 is 11, making option A correct.
* What happens after the log (for understanding, not affecting the answer) After the main script finishes, the event loop processes the timer callback for setInterval:
Callback:
() = > {
total++; // from 11 to 12
clearInterval(interval); // cancels further executions
total++; // from 12 to 13
}
So eventually total becomes 13, but this happens after console.log(total) has already executed. Since the question asks specifically for the output at line 08, the asynchronous updates do not change that line's output.
* Why other options are incorrect
* Option B (12): This would require the callback to run before the log, which does not happen because asynchronous callbacks are queued and executed after the current stack finishes.
* Option C (10): Ignores the total++ on line 07.
* Option D (13): This is the final value after the callback finishes, but it occurs after the console.log line executes, not at the time line 08 runs.
JavaScript knowledge references (descriptive, no links):
* JavaScript is single-threaded and uses an event loop with a call stack and task queues.
* setInterval schedules callbacks to run asynchronously after a minimum delay; the callback never runs before the current synchronous code finishes.
* Synchronous statements like total++ on line 07 execute before any queued interval callback.

Musume 2026-06-23 06:47:23

コメント

正解:
?」こちらは投票コメントになっております。普通のコメントに切り替えます。
ニックネーム: 送信 キャンセル
投票コメントをあげるごとに、選択した解答の投票数を1つ増やすことができます。

他人の解答コメントを賛成するのも、その解答に一票を入れることになります。したがって、すでに同じ意見の投票コメントが存在する場合、新規コメントをする代わりに賛成することもできます。

弊社を連絡する

我々は12時間以内ですべてのお問い合わせを答えます。

オンラインサポート時間:( UTC+9 ) 9:00-24:00
月曜日から土曜日まで

サポート:現在連絡