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){
    console.log(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. 

divideTwoNumbers(100/10);

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

function say Meow(){
    console.log("Meow");
}

Meow();






Post a Comment

0 Comments

React