- Learning JavaScript Data Structures and Algorithms
- Loiane Groner
- 174字
- 2021-08-05 17:31:22
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);
- Mastering Concurrency Programming with Java 8
- HTML5+CSS3王者歸來(lái)
- Android開(kāi)發(fā)精要
- 垃圾回收的算法與實(shí)現(xiàn)
- Learn Scala Programming
- 零基礎(chǔ)學(xué)Java(第4版)
- 可解釋機(jī)器學(xué)習(xí):模型、方法與實(shí)踐
- 零基礎(chǔ)輕松學(xué)SQL Server 2016
- Building RESTful Python Web Services
- Hands-On Nuxt.js Web Development
- Python數(shù)據(jù)可視化之美:專業(yè)圖表繪制指南(全彩)
- 玩轉(zhuǎn).NET Micro Framework移植:基于STM32F10x處理器
- 算法圖解
- Python預(yù)測(cè)分析與機(jī)器學(xué)習(xí)
- Swift Essentials(Second Edition)