The Functions in the JavaScript Language - JavaScript: Academy: The Stress Free Way to Learning JavaScript Inside & Out (2016)

JavaScript: Academy: The Stress Free Way to Learning JavaScript Inside & Out (2016)

Chapter 8. The Functions in the JavaScript Language

Basically, functions are collections of reusable codes that can be invoked at any part of your program. That means you won’t have to write the same code multiple times. Functions help programmers in creating modular codes. They also allow programmers to divide a large program into smaller functions.

Just like any other programming language, JavaScript offers features that can help in writing modular codes through functions. You’ve probably seen functions such as write() and alert() in the codes given in this book. You can use these functions repeatedly, but they were written in JavaScript once.

This language also allows you to write your own functions. This chapter will teach you the basics of functions in JavaScript.

How to Define a Function

You have to define functions before you can use them. The most popular method of defining a function involves the use of function (i.e. a JavaScript keyword). After typing function, specify the name and parameters that you’d like to use. Then, add a block of JavaScript statement enclosed by braces.

Note: You may create a function without defining any parameter. Thus, the list of parameters may be empty.

Here is the syntax that you must use when defining a function:

How to Call a Function

To call a function in your scripts, you just have to specify the function’s name. You may use the code below as an example:

That code will give you this result:

The Parameters of a Function

The functions you’ve seen so far don’t have any parameter. However, you can easily assign various parameters while invoking/calling a function. You can capture these parameters within the function. Once captured, you may modify those parameters according to your needs.

Return Statements

JavaScript allows you to add a return statement to your functions. This return statement is completely optional, although you have to use it if you want to acquire values from your function/s. In general, you should place the return statement at the last section of your function.

For instance, you may pass two values into your function. Then, you may expect that function to multiply the values and show the result in the program you are calling.

The Nested Functions

JavaScript allows you to place a function inside another function. This process is called“nesting a function.” As a general rule, you can’t include a function definition inside loops or conditional statements.

The code below will teach you how to nest your own functions:

The code above gives the following result:

How to Create Functions using“Function()”

Aside from the function statement discussed above, you may also create functions by using Function() (i.e. a JavaScript constructor) and new (i.e. a JavaScript operator).

To use this constructor, you must use the following syntax:

This syntax can contain any amount of string arguments. The final argument serves as the function’s body– it may hold arbitrary statements that are separated by semicolons.

Important Note: Function()doesn’t specify names for the functions it generates. Programmers refer to these unnamed functions as“anonymous” functions.