How to use the subscribe method in JavaScript

In this article, we will uncover the full potential of asynchronous data handling in JavaScript with our comprehensive guide to using the .subscribe() method.

Don’t miss out on this opportunity to enhance you knowledge in your JavaScript skills.

With out further ado, let’s begin.

What is subscribe in JavaScript?

The .subscribe() method or function in JavaScript is used to listen and respond to an Observable.

An Observable is a tool that helps us handle data that comes in asynchronously, like when we get data from an API.

When we use .subscribe(), we provide an object called an observer that tells us what to do with the data that the Observable sends us.

For instance, let’s say we have an Observable that fetches a list of tasks from an API. We can use .subscribe() to say what we want to happen when we receive the data successfully.

We can also define what to do if there’s an error or when the Observable is done sending data.

The .subscribe() method is similar to the .then(), .catch(), and .finally() methods of a Promise, but instead of dealing with Promises, it works with Observables.

If you want to perform certain actions when there is an error (similar to .catch()) or when the task is completed successfully (similar to .finally()).

You can include that logic within the subscribe method, as demonstrated below:

Observable.subscribe(
  value => toDoOnlyOnSuccess(value),
  error => toDoOnlyOnError(error),
  () => somethingtoDoAnyCase()
);

In a simple understanding, the .subscribe() method is a JavaScript object that determines how you handle the notifications you receive.

When you use the subscribe() function, it provides you with a Subscription object. This object has an unsubscribe() method that you can use to stop receiving notifications.

How to use the .subscribe() method in JavaScript?

To use the .subscribe() method in JavaScript, you first need an Observable.

An Observable helps you manage data that comes in asynchronously, like when you get data from an API.

You can create an Observable using the Observable constructor or by using the ready-made methods provided by the RxJS library.

Here’s the example:

// create an Observable that emits a value after 1 second
const myObservable = new Observable((observer) => {
  setTimeout(() => {
    observer.next("Hi, welcome to Itsourcecode!');
    observer.complete();
  }, 1000);
});

// subscribe to the Observable
myObservable.subscribe(
  (value) => {
    // handle emitted value
    console.log(value);
  },
  (error) => {
    // handle error
    console.error(error);
  },
  () => {
    // handle completion
    console.log('Observable completed');
  }
);

In this particular case, we generate an Observable that sends out a message (“Hi, welcome to Itsourcecode!”) after a 1-second delay and then finishes.

Next, we subscribe to this Observable using the .subscribe() method, where we pass three callback functions as arguments.

The first callback function executes when the Observable emits a value, the second runs in case of an error, and the third is triggered when the Observable completes.

Output:

Hi, welcome to Itsourcecode!
Observable completed

Conclusion

In conclusion, this article has provided a comprehensive guide to using the .subscribe() method in JavaScript for asynchronous data handling.

The .subscribe() method is used to listen and respond to an Observable, which helps manage data that comes in asynchronously, such as data from an API.

The article explained how to use the .subscribe() method by creating an Observable and subscribing to it with callback functions for handling emitted values, errors, and completion.

By utilizing the .subscribe() method effectively, you can enhance your JavaScript skills and efficiently handle asynchronous data in their applications.

We are hoping that this article provides you with enough information that helps you understand to subscribe in JavaScript.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Quick step-by-step summary (click to expand)
  1. What is subscribe in JavaScript. Read the ‘What is subscribe in JavaScript?’ section for the details and code.
  2. How to use the .subscribe() method in JavaScript. Read the ‘How to use the .subscribe() method in JavaScript?’ section for the details and code.
  3. Conclusion. Read the ‘Conclusion’ section for the details and code.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Caren Bautista


Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel
 · View all posts by Caren Bautista →

Leave a Comment