JS beginner to expert, study notes-4 (sec-9 loops)

For-in loop is like foreach loop of php. It iterates through any object or array….

  1. For-in loop iterates through an object.
    // for-in loop in Objects
    
    var person ={
    	name: "sajib",
    	surname: "biswas",
    	age: 34
    };
    
    for (var property in person){
    	alert(property);			//Alerts name / surname / age
    	alert(property[property]);	//Alerts sajib / biswas / 34
    }
    
    // for-in loop in Array
    
    courses = ["sass", "less", "angular"];
    
    for (var property in courses){
    	alert(property);			//0, 1, 2  
    	alert(courses[property]);	// sass, less, angular 
    }

     

  2. When a function is invoked, an Object called arguments is created inside that function which contains all parameters which were sent…
    // Arguments Object is found inside any function. It holds all arguments which were received by that function 
    
    function addNumbers(){
    	
    	var sum = 0;
    
    	for (var property in arguments){
    		sum += arguments[property];
    	}
    	
    	return sum;
    }
    
    alert( addNumbers(5, 5, 10) );

     

Leave a Reply

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