JS beginner to expert, study notes-1 (sec-1~5: basics & function)

This part is a summary of what i see some trivial basics of JS and its functions

  1. If we add script tag with codes in the HTML/php file, the js codes wont be cashed. For cashing, we need to link a external .js file.
  2. we can add two attribute to script tag “async” & “defer”. ByPJ_-uIUAAM98q javascript-defer-async-8-638

Section-6  (Functions)

  1. We can invoke a function before declaration. It is explained by hoisting.
  2. In js, functions are first class value. Means we can assign a function to a variable. pass functions as argument and also return a function from inside another function.
  3. In 1st phase, js compiler goes top to bottom and takes all function and variable declaration and allocates memory for them and in 2nd phase and it starts execution.
    alert(a);
    var a = 5;
    
    // compile phase:
    var a; //memory allocated but 5 is not assigned yet
    
    // then in execution phase it will again start execution from top. when it comes to line 1 it already knows that it has a variable named a but its value is not defined yet. so it will show 'undefined'
    

     

  4. For the both functions below, the “bar” will be local variable of the function. For creating local variable inside a function we have to use “var” keyword or use the variable as argument.
    function foo(bar){
    	
    }
    
    function foo(){
        var bar;
    }

    the following variable declaration will create a “num” variable in the global namespace.

    function foo(){
        bar = 5;
    }
  5. returning function from functions:
    var func = function(designation){
    	if(designation === "boss"){
    		return function(name){
    			alert("Hello Boss " + name);
    		}
    	}else
    		return function(name){
    			alert("Hello " + name);
    		}
    }
    
    var func2 = func();
    
    func2("sajib");
    

     

Leave a Reply

Your email address will not be published. Required fields are marked *