closure javascript

JavaScript
var counter = (function() {
  var privateCounter = 0;
  function changeBy(val) {
    privateCounter += val;
  }
  return {
    increment: function() {
      changeBy(1);
    },
    decrement: function() {
      changeBy(-1);
    },
    value: function() {
      return privateCounter;
    }
  };
})();

console.log(counter.value()); // logs 0
counter.increment();
counter.increment();
console.log(counter.value()); // logs 2
counter.decrement();
console.log(counter.value()); // logs 1
add()()function makeAdder(x) {
  return function(y) {
    return x + y;
  };
}

var add5 = makeAdder(5);
var add10 = makeAdder(10);

console.log(add5(2));  // 7
console.log(add10(2)); // 12function makeFunc() {
  var name = 'Mozilla';
  function displayName() {
    alert(name);
  }
  return displayName;
}

var myFunc = makeFunc();
myFunc();
body {
  font-family: Helvetica, Arial, sans-serif;
  font-size: 12px;
}

h1 {
  font-size: 1.5em;
}

h2 {
  font-size: 1.2em;
}
function bonjour(prenom) {

    let resultat = 'Bonjour ' + prenom ;
    let maClosure = () => console.log(resultat) ;
    return maClosure ;
           
}

let mafonction = bonjour('Rem\'s') ;
mafonction();

function timer() {
    let = secondes = 0

    let maclosure = () =>{
        return ++secondes ;

    }
    return maclosure ;
}

let montimer = timer()  ;
console.log(montimer()) ;
console.log(montimer()) ;
console.log(montimer()) ;
// premier timer
let monDeuxiemeTimer = timer()  ;
console.log(monDeuxiemeTimer()) ;
console.log(monDeuxiemeTimer()) ;
console.log(monDeuxiemeTimer()) ;
//deuxieme timer
console.log(montimer()) ;
//premier timer

mafonction() ;
// demo bonjour Rem's


Source

Also in JavaScript: