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

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);
主站蜘蛛池模板: 唐海县| 洛隆县| 民乐县| 彰化县| 遂溪县| 收藏| 防城港市| 镶黄旗| 马鞍山市| 乡城县| 定日县| 乐东| 璧山县| 司法| 东台市| 贵港市| 皋兰县| SHOW| 宝坻区| 合作市| 枣强县| 旌德县| 太仆寺旗| 天镇县| 赤峰市| 柯坪县| 岚皋县| 太白县| 黎城县| 江孜县| 浮梁县| 清河县| 左贡县| 高密市| 定日县| 黑山县| 仪陇县| 盱眙县| 平江县| 元江| 云梦县|