DEV Community

Cover image for "Understanding Callback Functions, Objects, and Synchronous Programming: A Comprehensive Guide"
Karthick (k)
Karthick (k)

Posted on

"Understanding Callback Functions, Objects, and Synchronous Programming: A Comprehensive Guide"

What is Synchronous JavaScript?#

In synchronous JavaScript, code is executed line by line, in the order it appears. Each operation must complete before the next one begins. This blocking behaviour means the JavaScript engine won’t move on to the next task until the current one is fully finished.

Asynchronous JavaScript

Asynchronous programming, on the other hand, allows multiple tasks to run independently of each other. In asynchronous code, a task can be initiated, and while waiting for it to complete, other tasks can proceed. This non-blocking nature helps improve performance and responsiveness, especially in web applications.

Asynchronous Concepts in JavaScript

Callbacks: Functions are passed as arguments to other functions & are executed after an operation completes.

Promises: Objects that represent an asynchronous operation’s eventual completion or failure.

Async/Await: Modern syntax for handling asynchronous operations by making code more readable.

Event Loop: Manages the execution of multiple tasks by enabling asynchronous behaviour.

What is the diff between process vs threads

1. Process:

Processes are basically the programs that are dispatched from the ready state and are scheduled in the CPU for execution. PCB (Process Control Block) holds the concept of the process. A process can create other processes which are known as Child Processes. The process takes more time to terminate, and it is isolated, which means it does not share the memory with any other process.

The process can have the following states new, ready, running, waiting, terminated, and suspended.

2. Thread:

A thread is a segment of a process which means a process can have multiple threads, and these multiple threads are contained within a process. A thread has three states: Running, Ready, and Blocked.

The thread takes less time to terminate than the process, but unlike the process, threads do not isolate.

JavaScript Callback Function: Syntax, Usage, and Examples

What is a JS Callback Function?

A callback is a function passed as an argument to another function, which is then invoked inside the outer function to complete some routine or action. It allows us to control the order of execution, especially for asynchronous tasks like fetching data or setting timers.

When to Use a JavaScript Callback Function

  • Asynchronous Operations – Callbacks help execute code only when an asynchronous task (like a network request) completes.

  • Event Handling – JavaScript uses callbacks for handling user interactions, such as button clicks.

  • Reusability and Modularity – Callbacks allow you to create more flexible and reusable functions.

Objects in JavaScript

An object is a dynamic data structure that stores related data as key-value pairs, where each key uniquely identifies its value.

  • The values of properties can be primitives, objects, or functions (known as methods when defined inside an object).

  • Objects are mutable, and dynamic properties can be added, modified, or deleted at any time.

  • Objects allow data grouping and encapsulation, making it easier to manage related information and behaviour together.

1. Creation Using Object Literal

The object literal syntax allows you to define and initialize an object with curly braces {}, setting properties as key-value pairs.

let obj = {
name: "Sourav",
age: 23,
job: "Developer"
};
console.log(obj);

{"name":"Sourav","age":23,"job":"Developer"}

2. Creation Using new Object() Constructor

`let obj = new Object();
obj.name= "Sourav",
obj.age= 23,
obj.job= "Developer"

console.log(obj);`

{"name":"Sourav","age":23,"job":"Developer"}

Top comments (0)