JavaScript functions

 JavaScript functions

JavaScript uses functions to break or divide large code into smaller reusable pieces.

We've already used them but, lets create our own functions to do something.

Let's create a function to divide two numbers. Let's write the function definition first and then call it anywhere we need. 

We first use the function keyword to tell JavaScript that it's a function. Then, we name our function, in our case, divideTwoNumbers. Between the opening and closing parentheses we are telling JavaScript that we are passing you two parameters called a and b. These are placeholders. Lastly, the definition which isn't hard to understand, it's within the opening and closing curly { } braces.

function divideTwoNumbers(a,b){

divideTwoNumbers(10,2); //Calling the function. 

While calling the functions we pass the required arguments. In our case, 10 and 2 are the arguments. These are the actual values.

If we want to reuse it with other numbers, we can do it easily. 


We can also have functions that do not accept any parameters.

function say Meow(){


Post a Comment