JSFiddle - React, Tailwind, and code Playground

by paercebal

JavaScript

var x = 42 ;

function foo(b)
{
    if(b) var x = 24 ;
    
    alert(x) ;
}

foo(false) ; // alerts "undefined", not "42"
foo(true) ;  // alerts "24"

// As the global x variable is never used,
// this proves the variable x inside the function
// foo exists even if its value is undefined

// so the "conditional declaration" is in truth
// a conditional initialization