Web worker

[edsanimate_start entry_animation_type= “flipInX” entry_delay= “1” entry_duration= “1” entry_timing= “ease-in” exit_animation_type= “” exit_delay= “” exit_duration= “” exit_timing= “” animation_repeat= “1” keep= “yes” animate_on= “load” scroll_offset= “” custom_css_class= “”]

The Web worker describes how the javaScript is executed in the background without affecting the user-interface scripts of the page.

The web worker is used effectively in multi core processors.

Types of web workers

  • Dedicated workers
  • Shared workers

Dedicated workers are accessible by the scripts from where they are loaded.

Shared workers are accessible by multiple scripts.

Sample code

The javaScript file using postMessage()

var num = prompt(“Enter a number”);

num = parseInt(num);

if (isNaN(num))


alert(“Enter a Number”);


else if (num == 0)


alert(“The number is zero”);


else if (num%2)


alert(“The number is odd”);




alert(“The number is even”);



The main program which calls postMessage() function

<!DOCTYPE html>



<title>web worker example<title>



<p>The number is even or odd <output id=”result”></output></p>


var worker = new worker(“evenorodd.js”);

worker.onmessage = function (event)


document.getElementById(“result”).textcontent = event.data;