Functions

If we have a piece of code and we want to run lot of time we can put then into the function and use it instead of using again and again.

JavaScript Function Syntax

  1. A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().
  2. Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).
  3. The parentheses may include parameter names separated by commas: (parameter1, parameter2, ...)
  4. he code to be executed, by the function, is placed inside curly brackets: {}

Example:

function calculateAge(birthYear){
return 2018 - birthYear;
}

var ageRoopak = calculateAge(1992);
var ageBrooke = calculateAge(1993);
var ageRamesh = calculateAge(1991);
console.log(ageRoopak, ageBrooke, ageRamesh);
function yearUntilRetirement(year, firstName) {
var age = calculateAge(year);
var retirment = 65 - age;
console.log(firstName + ' retires in ' + retirment + 'years.');
}

yearUntilRetirement(1992, 'Roopak');
yearUntilRetirement(1993, 'Brooke');
yearUntilRetirement(1991, 'Ramesh');


click 'Check' to see the answer