DEV Community

Cover image for JS101–1 Making Javascript Function Simple
Raheel Khan
Raheel Khan

Posted on • Originally published at mraheelkhan.Medium

JS101–1 Making Javascript Function Simple

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

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

Top comments (0)