惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
WordPress大学
WordPress大学
罗磊的独立博客
小众软件
小众软件
I
InfoQ
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hugging Face - Blog
Hugging Face - Blog
MyScale Blog
MyScale Blog
博客园 - 聂微东
Microsoft Security Blog
Microsoft Security Blog
H
Help Net Security
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园_首页
S
SegmentFault 最新的问题
博客园 - 三生石上(FineUI控件)
P
Proofpoint News Feed
博客园 - 司徒正美
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
Jina AI
Jina AI
N
Netflix TechBlog - Medium

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Synchronous and Asynchronous behavior of JavaScript
Tejas Khanolkar · 2026-06-05 · via DEV Community

Synchronous and Asynchronous Behavior of JavaScript

Synchronous Behavior of JavaScript

Before understanding asynchronous JavaScript, we first need to understand synchronous behavior.

Synchronous behavior means JavaScript executes code one statement at a time in the order in which it appears.

Let's look at an example:

function getRun() {
    console.log("I am running");
}

console.log("Hi");
console.log("I am doing work");
getRun();
console.log("Bye");

Enter fullscreen mode Exit fullscreen mode

Output:

Hi
I am doing work
I am running
Bye

Enter fullscreen mode Exit fullscreen mode

Here, JavaScript executes each statement one after another.

It does not skip a statement and come back later. It finishes the current work before moving to the next one.

This type of execution is called synchronous execution.


JavaScript is Single-Threaded

You may have heard that JavaScript is a single-threaded language.

But what does that mean?

A single-threaded language can perform only one task at a time.

Consider the following code:

console.log("Program Start"); // 1

let a = 20; // 2
let b = 10; // 3

console.log(a + b); // 4

console.log("Program End"); // 5

Enter fullscreen mode Exit fullscreen mode

JavaScript executes the code in this order:

1 → 2 → 3 → 4 → 5

Enter fullscreen mode Exit fullscreen mode

It completes statement 1 before moving to statement 2.

It completes statement 2 before moving to statement 3.

This happens because JavaScript has only one main thread for executing code.

So when we say JavaScript is single-threaded, we mean that it can execute only one task at a time.


Why Do We Need Asynchronous Behaviour?

Imagine you click a button on a website.

After clicking the button, the browser needs to:

  • Fetch data from a server
  • Wait for a timer
  • Access the user's location
  • Read data from local storage

Some of these operations can take time.

Now imagine JavaScript stopped everything and waited until those operations finished.

The webpage would freeze.

Buttons would stop responding.

Animations would stop.

The user experience would become poor.

To avoid this problem, JavaScript uses asynchronous behavior.


What is Asynchronous Behaviour?

Asynchronous behavior allows JavaScript to start a task that may take time and continue executing the remaining code without waiting for that task to finish.

In other words, JavaScript does not block the execution of the rest of the program while waiting for certain operations to complete.

Let's see an example.

console.log("Hi");

setTimeout(function () {
    console.log("Run after 2 seconds");
}, 2000);

console.log("Bye");

Enter fullscreen mode Exit fullscreen mode

Many beginners expect the output to be:

Hi
Run after 2 seconds
Bye

Enter fullscreen mode Exit fullscreen mode

But the actual output is:

Hi
Bye
Run after 2 seconds

Enter fullscreen mode Exit fullscreen mode

Why?

Because JavaScript does not wait for the timer to finish.

Instead, it continues executing the next statement.

So "Bye" gets printed immediately.

After approximately 2 seconds, the callback function executes and prints:

Run after 2 seconds

Enter fullscreen mode Exit fullscreen mode


What If the Delay Is 0?

Let's change the timer value.

console.log("Hi");

setTimeout(function () {
    console.log("Run after 0 seconds");
}, 0);

console.log("Bye");

Enter fullscreen mode Exit fullscreen mode

Output:

Hi
Bye
Run after 0 seconds

Enter fullscreen mode Exit fullscreen mode

Many developers are surprised by this.

Even though the delay is 0 milliseconds, "Bye" is still printed before the callback function.

This shows that JavaScript does not execute the callback immediately.

The callback is executed later.

We will learn exactly how this happens in the next blog.


How Does JavaScript Perform This Asynchronous Work?

Operations such as:

  • Timers (setTimeout)
  • User interactions
  • Network requests
  • Location-related operations
  • Local storage operations

are not handled directly by the JavaScript engine.

The browser provides special features that help perform these tasks.

When JavaScript encounters such work, it can hand over that responsibility and continue executing the remaining code.

Once that work is completed, JavaScript gets notified and can execute the corresponding callback function.

The complete internal process behind this behavior will be discussed in the next blog.


Summary

  • Synchronous execution means code runs one statement after another.
  • JavaScript is single-threaded, meaning it executes one task at a time.
  • Some operations take time to complete.
  • Waiting for such operations would make the webpage unresponsive.
  • Asynchronous behavior allows JavaScript to continue running other code while those operations are being handled.
  • setTimeout() is a simple example of asynchronous behavior.
  • In the next blog, we will learn how JavaScript actually manages asynchronous tasks behind the scenes.