JSFiddle - React, Tailwind, and code Playground

by bouillard

HTML

<button id="wo">Without var</button>
<button id="with">With var</button>

JavaScript

x=1;
$('#wo').click(function() {
    a = x++;
    setTimeout(function() {
        alert(a);
    },1000);
});
$('#wo').click(function() {
    a = x++;
    setTimeout(function() {
        alert(a);
    },1000);
});


$('#with').click(function() {
    var a = x++;
    setTimeout(function() {
        alert(a);
    },1000);
});
$('#with').click(function() {
    var a = x++;
    setTimeout(function() {
        alert(a);
    },1000);
});