Functions in JavaScript
JavaScript Functions is a block of code that is designed to perform a specific task. Functions can be defined using the function
keyword, followed by a name. The name of the function is used to call it later.
Here is the syntax of a JavaScript Function:
function functionName() {
// code to be executed
}
Code Explanation
- function:- The
function
is the keyword that is used to define a function in JavaScript. - functionName:- The
functionName
is the name of the function. It should be a meaningful name that describes what the function does. // code to be executed
:- This is the code that the function will execute when it is called.
Defining a Function in JavaScript
Now that we know the basic syntax of a JavaScript function, let’s take a look at how to define a function. Here is an example:
Example
<script> function hello() { document.write("Hello user, did you enjoy learning JavaScript") } hello(); </script>
Explanation
In an above example, the function
namely hello
contains some text. We can call this function by it’s name hello();
and we will get the text that is written inside the function as output.
One of the main advantage of the function is that we can call it anywhere in the code, and as many times we want.
Example
<script> function hello() { document.write("Hello user, did you enjoy learning JavaScript") } hello(); document.write("<br>") var a = 10; if (a > 1){ document.write("yoo") } else { document.write("Nooo") } document.write("<br>") document.write("Some Text") document.write("<br>") document.write("Some different Text") document.write("<br>") hello(); </script>
In an above code, we first define a hello
function, then we call it. After that we have write some lines of code and then we again call that function. You can call the function anywhere and as many times you want. This is the main advantage of the function.
Parameters in JavaScript Functions
A JavaScript Function can take one or more parameters. Parameters are used to pass values into the function, which can then be used to perform a specific task. Here is an example of a function that takes two parameters:
Example
<script> function addNumbers(num1, num11) { var sum = num1 + num11; return sum; } var result = addNumbers(5, 10); document.write(result); </script>
In an above example, we define a function, called addNumber
. That takes two parameters, num1
and num11
, and returns their sum. Then can call the function by passing two numbers as arguments, (5, 10). This will output 15
We can also take any string value as parameter. Here is an example:
Example
<script> function greeting(name) { document.write("Hello, " + name + "!"); } greeting("John"); </script>
Return Values in JavaScript Functions
In the previous example, we saw how a JavaScript function can return a value. When a function returns a value, it means that it has completed its task and is ready to pass a result back to the caller. In the example above, the addNumbers
function returns the sum of its two parameters.
Here is another example:
Example
<script> function calculateArea(radius) { var area = Math.PI * radius * radius; return area; } var area = calculateArea(5); document.write(area); </script>
In an above example, we define a function, called calculateArea
, that takes one parameter, radius
, and returns the area of a circle with that radius. Then we give the value to the area and call the function.
Share This Post!
One thought on “Functions in JavaScript”