What is difference between a Function Expression vs Function Declaration

Theart·2024년 9월 2일

function

In Javascript, a function is a process of performing an operaition or calcuating a value, wrapped in a block, and made into an execution unit


function declaration vs function expression

function funcDeclaration() {
    return 'A function declaration';
}

let funcExpression = function () {
    return 'A function expression';
}

Before discussing the function declaration method other than the function expression and the function declaration expression, let's first look at the difference between these two methods. The most difference between these two methods is whether or not they are hoisting

But, what is hoisting?

hoisting

hoisting refers to a feature of Javascript in which the function declaration works as if it were written at the beginning of the code.

let's check the example

hello() // hello

function hello() {
	console.log('hello')
}

hello() // hello

You probably think that the first hello() function does not work. Beacause in order to operate the hello() function, the function must be delcared in advance, beacause hello() was declared after the hello() operation.

However, it works!

Before hello() is declared, you would think that the outputted hello() function would not work. This is because in order to operate the hello() function, the function must be declared in advance, because hello() was declared after the hello() operation.

As such, a phenomenon that can be output before the hello() function is declared is called hoisting.

profile
Thank you for my life

0개의 댓글