JS101–1 Making Javascript Function Simple

Image by Nenad Mećava from Pixabay

Javascript functions are easy. Functions can perform calculations, manipulate data, and can store them. It is a block of code that executes when it receives a call.

Javascript function starts with a keyword function at start and name of the function after a keyword, it is called “function declaration” or function definition.

function nameOfFunction(){

The above function will print “You have just called a function”.

Dynamically, parameters can be passed to the function to provide values at run time. The parameters are unlike other languages, it does not require parameter data types explicitly. Javascript will automatically assign the data type to the variable or parameter.

function nameOfFunction(parameter){
return "Value of Parameter is : " + parameter;

Upon calling of above function it will print “Value of Parameter is : Dynamic Value”.

As earlier mentioned, the calculation can be performed from the javascript function, an example would be

function myFunction(a, b) {
// Function returns the product of a and b
return a * b;
}
// Function is called, return value
myFunction(45, 55);

Even more, you can perform more complex calculations as below

function toCelsius(fahrenheit) {
return (5/9) * (fahrenheit-32);
}

Javascript can perform almost any calculation and any valid mathematics expression.

Raheel Khan is founder of Mrasta Inc and Leading Web Developer who loves to convert ideas into business and provide great support to the newbies.