JSFiddle - React, Tailwind, and code Playground

by deepak sisodiya

JavaScript

// javascript program using closure
/* A closure is a function defined within another scope that has access to all the variables within the outer scope. */
/* Because JavaScript has no syntax to denote scope, we need to use closures to implement private members. */

var alert  = function (str) {
   var st = document.createTextNode(str);
   var p = document.createElement('p');
   p.appendChild(st);
   document.querySelector('body').appendChild(p);
}

function greeter(name , age) {
    var message = name + " is " + age + " years old"
    
    return function greet() {
        alert(message)
    }
}

var closure = greeter("deepak" , "22")

alert(typeof(closure))

closure();