what is javascript function?

      Javascript function is grouping a set of codes to perform particular operations. we will invoke or execute the javascript function according to our needs.

Javascript function syntax

            The javascript function can be created by using the keyword function followed by specifying function name then the arguments inside the parentheses , then we can open the curly brackets and can write function definition inside the curly brackets.

function function_name(parameter_1,parameter_2, ……….)
// function_definition

  • function is a keyword used to define functions.
  • function_name is the name we will define for the function, any name we can use and it is user defined. the function name can include letters, numbers and underscore.
  • parameter_1, parameter_2 are the parameters list. we can specify multiple parameters by separating by the comma.
  • function_definition is the code block , the code we need to execute when the function is invoked.

Example of javascript function

// The function returns the sum of number1 and number2
function AddTwoNumbers(number1,number2)
return number1+number2;

above you can see basic javascript example for finding the sum of two numbers.

  • function is the keyword.
  • AddTwoNumbers is the function name.
  • number1 and number2 are the parameters.
  • “return number1+number2” , add two number and return the sum.

How to call javascript function

        we can call javascript function by just specifying the function name and the arguments inside the parentheses.

Javascript function calling example

var sum=AddTwoNumbers(40,60);

  • “AddTwoNumbers” is the method name
  • “40,60” are the parameter values.
  • “sum” is just a variable used to store the function returning value

Finally after executing the above function (“var sum=AddTwoNumbers(40,60);”) we will get 100 as result in the “sum” variable.

Leave a Reply