LEARNING JAVASCRIPT - Trang 127

CHAPTER 6

Functions

A function is a self-contained collection of statements that run as a single unit: essen‐

tially, you can think of it as a subprogram. Functions are central to JavaScript’s power

and expressiveness, and this chapter introduces you to their basic usage and

mechanics.
Every function has a body; this is the collection of statements that compose the

function:

function

sayHello

() {

// this is the body; it started with an opening curly brace...

console

.

log

(

"Hello world!"

);

console

.

log

(

"¡Hola mundo!"

);

console

.

log

(

"Hallo wereld!"

);

console

.

log

(

"Привет мир!"

);

// ...and ends with a closing curly brace

}

This is an example of a function declaration: we’re declaring a function called

say

Hello

. Simply declaring a function does not execute the body: if you try this example,

you will not see our multilingual “Hello, World” messages printed to the console. To

call a function (also called running, executing, invoking, or dispatching), you use the

name of the function followed by parentheses:

sayHello

();

// "Hello, World!" printed to the

// console in different languages

103

Liên Kết Chia Sẽ

** Đây là liên kết chia sẻ bới cộng đồng người dùng, chúng tôi không chịu trách nhiệm gì về nội dung của các thông tin này. Nếu có liên kết nào không phù hợp xin hãy báo cho admin.