Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Can someone explain if these two different ways are the same or different?

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #265928
    Pranab
    Participant

    //with using closure

    function wrapValue(n){
                var localVar = n;
                return function(){ return localVar;};       
            }
    var wrap1 = wrapValue(12);
            var wrap2 = wrapValue(24);
    
            console.log(wrap1());//12
    

    // I will try and do this

    function wrapValue(n){
                return n;
            }
    
            var variable1 = wrapValue(12);
    
            console.log(variable1);//12
    
            var variable2 = wrapValue(24);
    
            console.log(variable2);//24
    

    So, those two are basically same or not and when do i use closure?

Viewing 1 post (of 1 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.