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(){
return "You have just called a 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;
}
nameOfFunction("Dynamic Value")
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);
// furthermore, the returned value can be store in variable
var value = myFunction(45,55)
// this will print 100 in console.
console.log(value)
Even more, you can perform more complex calculations as below
function toCelsius(fahrenheit) {
return (5/9) * (fahrenheit-32);
}
var celsiusValue = toCelsius(77);
console.log(celsiusValue)
Javascript can perform almost any calculation and any valid mathematics expression.
Top comments (0)