Functions in JavaScript - ProgrammerTech
Language:

Search

Functions in JavaScript

  • Share this:
Functions in JavaScript

Functions JavaScript

Functions JavaScript is a block of code that performs a set of tasks in a specified order. And it is a subprogram whose task is to perform a specific task when it is called (that is, it is a group of commands collected in one place and executed when we call it).

The general form of writing the JavaSccript function

function name(parameter){
   /Command
}
  • The function keyword is the keyword to define a function in JavaScript.
  • The word name is the name we give the function.
  • And between parentheses parameter if we want to pass values ​​and put parameters and separate them by a latin comma,.

We will explain to you about the onclick feature in the event that someone clicks on an image or a button on a page, and we want him to run the commands in a function. First we open the html file and add a button with the tag button. We created a button but it has no commands to be triggered when the button is clicked.

We will associate the button in a function like this:

<button type="button" onclick = "play()">Click here to play</button>

The code is in the javascript file

function play(){
   alert("true");
}

We created a function called play and the commands in this function when it is run is to print true in the popup dialer, because we have set the onclick property for when we press the button it starts working. 

As you can see, when we click on the button, execute the javascript commands in function. This type is without parameter.

 

Parameters of JavaScript

Most functions take parameters, which are a comma-separated string of variables. parameters can be defined as many as we want within the functions.

To define parameters in functions, take the form

function name(parameter1, parameter2, parameter3){
   /Command
}

We assign values ​​to parameters by passing them in parentheses to the function in a comma-separated string. The values ​​passed to the function are called arguments. Let's take an example when it has a parameter

<button type="button" onclick = "data('Muhammad', 1994,28)">Click here to play</button>

We created parameters in the javascript file and put their argoment values ​​in the html file. We displayed the data via the console with a javascript file.

Function parameter javascript

 

Return clause used with functions

Functions contain a return clause and consist of return followed by a value.

function name(parameter1, parameter2, parameter3){
   return 'Valuable'
}

When the compiler finds the Return clause, it will stop executing the subsequent instructions in the function, and return a value. It passes the value to the code that called the function.

function sum($saja, $muhammad) {
    return $saja + $muhammad;
}
var result = sum(27, 28);
console.log(result);

We created a function called sum, which means summing values, and passed two parameters to this function, namely saja and muhammad. Then we told the function via the return statement to return the sum of the two parameters. And we created a variable to give values ​​to the parameters and its name is result and passed to it 27 and 28. Then we printed the result from the console, and when we run the code, its result will be:

55

 

Nested functions in JavaScript

Nested functions In JavaScript, a function can contain another inner function inside a function, and it can be one or more inner functions that are within the scope of the outer function. This inner function can also access the variables of the outer functions. The outer function cannot access the variables inside the inner function.

Example

function mainFun($saja) {
    function nestedFun() {
        console.log($saja);
    }
    return nestedFun();
}
mainFun("sudan");

We create a main function mainFun and pass it the $saja parameter. And we created a nested function inside the main function which is nestedFun that prints the parameters in the main function via the console. Then the main function returns the inner function via return and then we pass a value to the main function which is sudan as the parameter value.

 

Anonymous JavaScript Function

JavaScript allows us to create anonymous functions without any name, and the use of anonymous functions is to assign them to a variable.

var sum = function ($saja, $muhammad) {
    console.log($saja + $muhammad);
}
sum(2, 3);

We created a variable, sum, and assigned this variable to a function with two parameters, saja and muhammad. Then we display the data via the console and the data is the sum of the two parameters found in the anonymous function. Then we pass values ​​for the parameters, which are 2 and 3.

calendar

5

Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com