官术网_书友最值得收藏!

Functions

Functions are very important when working with JavaScript. We will also use functions a lot in our examples.

The following code demonstrates the basic syntax of a function. It does not have arguments or the return statement:

function sayHello() {
    console.log('Hello!');
}

To call this code, we simply use the following call:

sayHello();

We can also pass arguments to a function. Arguments are variables with which a function is supposed to do something. The following code demonstrates how to use arguments with functions:

function output(text) {
    console.log(text);
}

To use this function, we can use the following code:

output('Hello!');

You can use as many arguments as you like, as follows:

output('Hello!', 'Other text');

In this case, only the first argument is used by the function and the second one is ignored.

A function can also return a value, as follows:

function sum(num1, num2) {
    return num1 + num2;
}

This function calculates the sum of two given numbers and returns its result. We can use it as follows:

var result = sum(1,2);
output(result);
主站蜘蛛池模板: 武陟县| 饶阳县| 庆安县| 英吉沙县| 白城市| 方山县| 宜君县| 项城市| 兴和县| 蕉岭县| 中阳县| 封开县| 泽普县| 炉霍县| 潜山县| 永德县| 白玉县| 隆林| 江西省| 延安市| 天气| 苏尼特右旗| 剑阁县| 易门县| 栾城县| 芦溪县| 绍兴市| 南安市| 徐汇区| 江阴市| 随州市| 衡水市| 清涧县| 隆林| 潼关县| 舟曲县| 祥云县| 昌乐县| 自贡市| 荆州市| 崇文区|