What does synchronous execution mean in JavaScript?
Synchronous execution means the code runs line by line, one after another. Each step waits for the previous one to finish before starting.
Click to reveal answer
beginner
What is asynchronous execution in JavaScript?
Asynchronous execution lets some code run in the background, so the program can keep doing other things without waiting for that code to finish.
Click to reveal answer
intermediate
Which JavaScript feature allows asynchronous code to run without blocking the main thread?
Promises and async/await allow asynchronous code to run without blocking the main thread, so the program stays responsive.
Click to reveal answer
beginner
Give an example of a synchronous operation.
A simple math calculation like adding two numbers is synchronous because it happens immediately and the next line waits for it to finish.
Click to reveal answer
beginner
Why is asynchronous execution important in web development?
It keeps the website responsive by not freezing the page while waiting for things like data from the internet or timers.
Click to reveal answer
What happens in synchronous JavaScript execution?
ACode runs one line at a time, waiting for each to finish
BCode runs multiple lines at the same time
CCode runs only after a timer
DCode runs only when user clicks
✗ Incorrect
Synchronous execution means each line waits for the previous one to finish before running.
Which of these is an example of asynchronous JavaScript?
AA for loop adding numbers
BFetching data from a server
CDeclaring a variable
DSimple math calculation
✗ Incorrect
Fetching data from a server happens asynchronously so the program doesn't freeze while waiting.
What JavaScript feature helps handle asynchronous code easily?
AFunctions
BVariables
CLoops
DPromises
✗ Incorrect
Promises help manage asynchronous operations by allowing code to run after a task finishes.
Why might synchronous code cause problems in a web page?
AIt uses too little memory
BIt runs too fast
CIt can freeze the page while waiting
DIt never finishes
✗ Incorrect
Synchronous code can block the page, making it unresponsive until the task finishes.
Which keyword is used to write asynchronous functions in JavaScript?
Aasync
Bsync
Cwait
Ddefer
✗ Incorrect
The 'async' keyword marks a function as asynchronous, allowing use of 'await' inside it.
Explain the difference between synchronous and asynchronous execution in JavaScript.
Think about how the program waits or doesn't wait for tasks to finish.
You got /4 concepts.
Why is asynchronous execution useful for web pages and user experience?
Consider what happens if the page waits for a slow task.
You got /4 concepts.
Practice
(1/5)
1. Which statement best describes synchronous execution in JavaScript?
easy
A. Tasks run only when the user clicks a button.
B. Tasks run in the background without waiting for others to finish.
C. Tasks run randomly without any order.
D. Tasks run one after another, waiting for each to finish before starting the next.
Solution
Step 1: Understand synchronous execution
Synchronous means tasks happen one by one, each waiting for the previous to finish.
Step 2: Match description to options
Tasks run one after another, waiting for each to finish before starting the next. clearly states tasks run one after another, which matches synchronous behavior.
Final Answer:
Tasks run one after another, waiting for each to finish before starting the next. -> Option D
Quick Check:
Synchronous = sequential task execution [OK]
Hint: Synchronous means wait for each task to finish before next [OK]
Common Mistakes:
Confusing synchronous with asynchronous execution
Thinking tasks run at the same time in synchronous
Believing synchronous tasks run randomly
2. Which of the following is the correct syntax to create a simple asynchronous function in JavaScript?
easy
A. function async myFunction() {}
B. async function myFunction() {}
C. function myFunction async() {}
D. function myFunction() async {}
Solution
Step 1: Recall async function syntax
In JavaScript, the keyword 'async' comes before the function keyword to declare an asynchronous function.
Step 2: Check each option
async function myFunction() {} uses 'async function myFunction() {}' which is correct syntax.
Final Answer:
async function myFunction() {} -> Option B
Quick Check:
Async keyword before function = correct syntax [OK]
Hint: Async keyword goes before function keyword [OK]
Step 1: Understand synchronous and asynchronous parts
'console.log("Start")' runs immediately (synchronous). 'setTimeout' schedules 'Middle' to run later (asynchronous). 'console.log("End")' runs immediately after 'Start'.
Step 2: Determine order of output
Output order is 'Start', then 'End', then 'Middle' because 'setTimeout' callback runs after current code finishes.
Final Answer:
Start
End
Middle -> Option A
Quick Check:
setTimeout delays callback, so logs after synchronous code [OK]
Hint: setTimeout callback runs after current code finishes [OK]
Common Mistakes:
Assuming setTimeout runs immediately
Thinking output order matches code order exactly
Ignoring asynchronous delay even if zero
4. Identify the error in this code snippet that tries to use asynchronous execution:
function fetchData() {
let data;
fetch('https://api.example.com/data')
.then(response => response.json())
.then(json => data = json);
return data;
}
console.log(fetchData());
medium
A. Syntax error in arrow functions
B. fetch is not a function
C. fetchData returns before data is assigned, so returns undefined
D. Missing async keyword on fetchData
Solution
Step 1: Analyze asynchronous fetch behavior
fetch returns a promise and .then callbacks run later, but fetchData returns immediately before data is set.
Step 2: Understand return value timing
Since data is assigned asynchronously, fetchData returns undefined because data is not ready yet.
Final Answer:
fetchData returns before data is assigned, so returns undefined -> Option C
Quick Check:
Async assignment happens after return, so undefined returned [OK]
Hint: Return happens before async data arrives, so returns undefined [OK]
Common Mistakes:
Thinking fetchData waits for fetch to finish
Assuming data is ready immediately
Confusing syntax errors with logic errors
5. You want to run three tasks: A, B, and C. Task B depends on A finishing, but C can run anytime. Which approach correctly uses asynchronous execution to run tasks efficiently?
hard
A. Run A asynchronously, then run B in A's callback, and run C asynchronously at the same time as A.
B. Run B and C asynchronously first, then run A synchronously after both finish.
C. Run A synchronously, then B synchronously, then C synchronously after both.
D. Run all tasks synchronously one after another.
Solution
Step 1: Understand task dependencies
Task B depends on A finishing, so B must wait for A. Task C can run anytime, so it can run in parallel.
Run A asynchronously, then run B in A's callback, and run C asynchronously at the same time as A. runs A asynchronously, then runs B in A's callback (after A finishes), and runs C asynchronously at the same time as A, maximizing efficiency.
Final Answer:
Run A asynchronously, then run B in A's callback, and run C asynchronously at the same time as A. -> Option A
Quick Check:
Respect dependencies and run independent tasks in parallel [OK]
Hint: Run dependent tasks in callbacks, independent tasks in parallel [OK]
Common Mistakes:
Running dependent tasks in parallel ignoring order