DEV Community

loading...
Cover image for JS101–1 Making Javascript Function Simple

JS101–1 Making Javascript Function Simple

Raheel Khan
Full-Stack Dev | Software Engineer | Content Creator | Proud Pukhtoon
Originally published at mraheelkhan.Medium ・1 min read

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()
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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

Discussion (0)