[TIL]JavaScript > Functions

Agnes Shin·2022년 2월 8일

Function Declarations
In JavaScript, there are many ways to create a function. One way to create a function is by using a function declaration. Just like now a variable declaration binds a value to a variable name, a function declaration binds a function to a name, or an identifier. Take a look at the anatomy of a function declaration below:

FUNCTION KEYWORD is function, IDENTIFIER is greetWorld() {
functiongreetWorld() {
	console.log(‘Hello, World!’);

	Function body

A function declaration consists of:
The function keyword.
The name of the function, or its identifier, followed by parentheses.
A function body, or the block of statements required to perform a specific task, enclosed in the function’s curly brackets, { }.
A function declaration is a function that is bound to an identifier, or name. In the next exercise we’ll go over how to run the code inside the function body.
We should also be aware of the hoisting feature in JavaScript which allows access to function declarations before they’re defined.
Take a look at example of hoisting:

greetWorld(); //outPut: Hello, World!
function greetWorld() {
	console.log(‘Hello, World!’);

Notice how hoisting allowed greetWorld() to be called before the greetWorld() function was defined! Since hoisting isn’t considered good practice, we simply want you to be aware of this feature.

Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope , prior to execution of the code.
Hoisting allows functions to be safely used in code before they are declared.
Variables and class declarations are also hoisted, so that they too can be referenced before they are declared. Note that doing so can lead to unexpected errors, and is not generally recommended.

30기 신윤숙 / FE

0개의 댓글