Day 3 in JavaScript



This content originally appeared on DEV Community and was authored by VIDHYA VARSHINI

Function:
• A function is a set of instructions with a name.
• Functions are reusable blocks of code designed to perform a particular task.
• Functions enable better code organization, modularity, and efficiency.
• They can be used to avoid repeating the same code.

How to define a function:
• function is a reserved keyword.
• Syntax: function functionName(parameters) { // instructions }

Example:

function MakeBiriyani() {
set of instructions
}

Here, ‘MakeBiriyani’ is the function name and inside ‘()’, the parameters can be given. This is to get the input for the function.
‘{}’ contains the set of instructions.

Example:

function sample() {
  console.log(typeof 1);
}
sample();

Here, the term ‘typeof’ will give the data type. It will return as “number”.

Another example:

function sub(a, b) {
  console.log("sub: " + (a - b));
}
sub(12, 7);

Output: 5


This content originally appeared on DEV Community and was authored by VIDHYA VARSHINI