DEV Community

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

Posted on • Originally published at mraheelkhan.Medium

3 1

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";
}
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.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay