âčïž Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://javascript.info/function-expressions |
| Last Crawled | 2026-04-16 09:20:21 (1 day ago) |
| First Indexed | 2019-11-06 11:39:04 (6 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Function expressions |
| Meta Description | null |
| Meta Canonical | null |
| Boilerpipe Text | In JavaScript, a function is not a âmagical language structureâ, but a special kind of value.
The syntax that we used before is called a
Function Declaration
:
function
sayHi
(
)
{
alert
(
"Hello"
)
;
}
There is another syntax for creating a function that is called a
Function Expression
.
It allows us to create a new function in the middle of any expression.
For example:
let
sayHi
=
function
(
)
{
alert
(
"Hello"
)
;
}
;
Here we can see a variable
sayHi
getting a value, the new function, created as
function() { alert("Hello"); }
.
As the function creation happens in the context of the assignment expression (to the right side of
=
), this is a
Function Expression
.
Please note, thereâs no name after the
function
keyword. Omitting a name is allowed for Function Expressions.
Here we immediately assign it to the variable, so the meaning of these code samples is the same: âcreate a function and put it into the variable
sayHi
â.
In more advanced situations, that weâll come across later, a function may be created and immediately called or scheduled for a later execution, not stored anywhere, thus remaining anonymous.
Function is a value
Letâs reiterate: no matter how the function is created, a function is a value. Both examples above store a function in the
sayHi
variable.
We can even print out that value using
alert
:
function
sayHi
(
)
{
alert
(
"Hello"
)
;
}
alert
(
sayHi
)
;
// shows the function code
Please note that the last line does not run the function, because there are no parentheses after
sayHi
. There are programming languages where any mention of a function name causes its execution, but JavaScript is not like that.
In JavaScript, a function is a value, so we can deal with it as a value. The code above shows its string representation, which is the source code.
Surely, a function is a special value, in the sense that we can call it like
sayHi()
.
But itâs still a value. So we can work with it like with other kinds of values.
We can copy a function to another variable:
function
sayHi
(
)
{
// (1) create
alert
(
"Hello"
)
;
}
let
func
=
sayHi
;
// (2) copy
func
(
)
;
// Hello // (3) run the copy (it works)!
sayHi
(
)
;
// Hello // this still works too (why wouldn't it)
Hereâs what happens above in detail:
The Function Declaration
(1)
creates the function and puts it into the variable named
sayHi
.
Line
(2)
copies it into the variable
func
. Please note again: there are no parentheses after
sayHi
. If there were, then
func = sayHi()
would write
the result of the call
sayHi()
into
func
, not
the function
sayHi
itself.
Now the function can be called as both
sayHi()
and
func()
.
We could also have used a Function Expression to declare
sayHi
, in the first line:
let
sayHi
=
function
(
)
{
// (1) create
alert
(
"Hello"
)
;
}
;
let
func
=
sayHi
;
//(2)
// ...
Everything would work the same.
Why is there a semicolon at the end?
You might wonder, why do Function Expressions have a semicolon
;
at the end, but Function Declarations do not:
function
sayHi
(
)
{
// ...
}
let
sayHi
=
function
(
)
{
// ...
}
;
The answer is simple: a Function Expression is created here as
function(âŠ) {âŠ}
inside the assignment statement:
let sayHi = âŠ;
. The semicolon
;
is recommended at the end of the statement, itâs not a part of the function syntax.
The semicolon would be there for a simpler assignment, such as
let sayHi = 5;
, and itâs also there for a function assignment.
Callback functions
Letâs look at more examples of passing functions as values and using function expressions.
Weâll write a function
ask(question, yes, no)
with three parameters:
question
Text of the question
yes
Function to run if the answer is âYesâ
no
Function to run if the answer is âNoâ
The function should ask the
question
and, depending on the userâs answer, call
yes()
or
no()
:
function
ask
(
question
,
yes
,
no
)
{
if
(
confirm
(
question
)
)
yes
(
)
else
no
(
)
;
}
function
showOk
(
)
{
alert
(
"You agreed."
)
;
}
function
showCancel
(
)
{
alert
(
"You canceled the execution."
)
;
}
// usage: functions showOk, showCancel are passed as arguments to ask
ask
(
"Do you agree?"
,
showOk
,
showCancel
)
;
In practice, such functions are quite useful. The major difference between a real-life
ask
and the example above is that real-life functions use more complex ways to interact with the user than a simple
confirm
. In the browser, such functions usually draw a nice-looking question window. But thatâs another story.
The arguments
showOk
and
showCancel
of
ask
are called
callback functions
or just
callbacks
.
The idea is that we pass a function and expect it to be âcalled backâ later if necessary. In our case,
showOk
becomes the callback for âyesâ answer, and
showCancel
for ânoâ answer.
We can use Function Expressions to write an equivalent, shorter function:
function
ask
(
question
,
yes
,
no
)
{
if
(
confirm
(
question
)
)
yes
(
)
else
no
(
)
;
}
ask
(
"Do you agree?"
,
function
(
)
{
alert
(
"You agreed."
)
;
}
,
function
(
)
{
alert
(
"You canceled the execution."
)
;
}
)
;
Here, functions are declared right inside the
ask(...)
call. They have no name, and so are called
anonymous
. Such functions are not accessible outside of
ask
(because they are not assigned to variables), but thatâs just what we want here.
Such code appears in our scripts very naturally, itâs in the spirit of JavaScript.
A function is a value representing an âactionâ
Regular values like strings or numbers represent the
data
.
A function can be perceived as an
action
.
We can pass it between variables and run when we want.
Function Expression vs Function Declaration
Letâs formulate the key differences between Function Declarations and Expressions.
First, the syntax: how to differentiate between them in the code.
Function Declaration:
a function, declared as a separate statement, in the main code flow:
// Function Declaration
function
sum
(
a
,
b
)
{
return
a
+
b
;
}
Function Expression:
a function, created inside an expression or inside another syntax construct. Here, the function is created on the right side of the âassignment expressionâ
=
:
// Function Expression
let
sum
=
function
(
a
,
b
)
{
return
a
+
b
;
}
;
The more subtle difference is
when
a function is created by the JavaScript engine.
A Function Expression is created when the execution reaches it and is usable only from that moment.
Once the execution flow passes to the right side of the assignment
let sum = functionâŠ
â here we go, the function is created and can be used (assigned, called, etc. ) from now on.
Function Declarations are different.
A Function Declaration can be called earlier than it is defined.
For example, a global Function Declaration is visible in the whole script, no matter where it is.
Thatâs due to internal algorithms. When JavaScript prepares to run the script, it first looks for global Function Declarations in it and creates the functions. We can think of it as an âinitialization stageâ.
And after all Function Declarations are processed, the code is executed. So it has access to these functions.
For example, this works:
sayHi
(
"John"
)
;
// Hello, John
function
sayHi
(
name
)
{
alert
(
`
Hello,
${
name
}
`
)
;
}
The Function Declaration
sayHi
is created when JavaScript is preparing to start the script and is visible everywhere in it.
âŠIf it were a Function Expression, then it wouldnât work:
sayHi
(
"John"
)
;
// error!
let
sayHi
=
function
(
name
)
{
// (*) no magic any more
alert
(
`
Hello,
${
name
}
`
)
;
}
;
Function Expressions are created when the execution reaches them. That would happen only in the line
(*)
. Too late.
Another special feature of Function Declarations is their block scope.
In strict mode, when a Function Declaration is within a code block, itâs visible everywhere inside that block. But not outside of it.
For instance, letâs imagine that we need to declare a function
welcome()
depending on the
age
variable that we get during runtime. And then we plan to use it some time later.
If we use Function Declaration, it wonât work as intended:
let
age
=
prompt
(
"What is your age?"
,
18
)
;
// conditionally declare a function
if
(
age
<
18
)
{
function
welcome
(
)
{
alert
(
"Hello!"
)
;
}
}
else
{
function
welcome
(
)
{
alert
(
"Greetings!"
)
;
}
}
// ...use it later
welcome
(
)
;
// Error: welcome is not defined
Thatâs because a Function Declaration is only visible inside the code block in which it resides.
Hereâs another example:
let
age
=
16
;
// take 16 as an example
if
(
age
<
18
)
{
welcome
(
)
;
// \ (runs)
// |
function
welcome
(
)
{
// |
alert
(
"Hello!"
)
;
// | Function Declaration is available
}
// | everywhere in the block where it's declared
// |
welcome
(
)
;
// / (runs)
}
else
{
function
welcome
(
)
{
alert
(
"Greetings!"
)
;
}
}
// Here we're out of curly braces,
// so we can not see Function Declarations made inside of them.
welcome
(
)
;
// Error: welcome is not defined
What can we do to make
welcome
visible outside of
if
?
The correct approach would be to use a Function Expression and assign
welcome
to the variable that is declared outside of
if
and has the proper visibility.
This code works as intended:
let
age
=
prompt
(
"What is your age?"
,
18
)
;
let
welcome
;
if
(
age
<
18
)
{
welcome
=
function
(
)
{
alert
(
"Hello!"
)
;
}
;
}
else
{
welcome
=
function
(
)
{
alert
(
"Greetings!"
)
;
}
;
}
welcome
(
)
;
// ok now
Or we could simplify it even further using a question mark operator
?
:
let
age
=
prompt
(
"What is your age?"
,
18
)
;
let
welcome
=
(
age
<
18
)
?
function
(
)
{
alert
(
"Hello!"
)
;
}
:
function
(
)
{
alert
(
"Greetings!"
)
;
}
;
welcome
(
)
;
// ok now
When to choose Function Declaration versus Function Expression?
As a rule of thumb, when we need to declare a function, the first thing to consider is Function Declaration syntax. It gives more freedom in how to organize our code, because we can call such functions before they are declared.
Thatâs also better for readability, as itâs easier to look up
function f(âŠ) {âŠ}
in the code than
let f = function(âŠ) {âŠ};
. Function Declarations are more âeye-catchingâ.
âŠBut if a Function Declaration does not suit us for some reason, or we need a conditional declaration (weâve just seen an example), then Function Expression should be used.
Summary
Functions are values. They can be assigned, copied or declared in any place of the code.
If the function is declared as a separate statement in the main code flow, thatâs called a âFunction Declarationâ.
If the function is created as a part of an expression, itâs called a âFunction Expressionâ.
Function Declarations are processed before the code block is executed. They are visible everywhere in the block.
Function Expressions are created when the execution flow reaches them.
In most cases when we need to declare a function, a Function Declaration is preferable, because it is visible prior to the declaration itself. That gives us more flexibility in code organization, and is usually more readable.
So we should use a Function Expression only when a Function Declaration is not fit for the task. Weâve seen a couple of examples of that in this chapter, and will see more in the future. |
| Markdown | EN
- [ARŰč۱ۚÙ](https://ar.javascript.info/function-expressions)
- [ENEnglish](https://javascript.info/function-expressions)
- [ESEspañol](https://es.javascript.info/function-expressions)
- [FAÙۧ۱۳Û](https://fa.javascript.info/function-expressions)
- [FRFrançais](https://fr.javascript.info/function-expressions)
- [IDIndonesia](https://id.javascript.info/function-expressions)
- [ITItaliano](https://it.javascript.info/function-expressions)
- [JAæ„æŹèȘ](https://ja.javascript.info/function-expressions)
- [KOíê”ìŽ](https://ko.javascript.info/function-expressions)
- [RUĐ ŃŃŃĐșĐžĐč](https://learn.javascript.ru/function-expressions)
- [TRTĂŒrkçe](https://tr.javascript.info/function-expressions)
- [UKĐŁĐșŃаŃĐœŃŃĐșа](https://uk.javascript.info/function-expressions)
- [UZOʻzbek](https://uz.javascript.info/function-expressions)
- [ZHçźäœäžæ](https://zh.javascript.info/function-expressions)
We want to make this open-source project available for people all around the world.
[Help to translate](https://javascript.info/translate) the content of this tutorial to your language\!
[](https://javascript.info/)
[BuyEPUB/PDF](https://javascript.info/ebook)
[Tutorial map](https://javascript.info/tutorial/map)
Light themeDark theme
Share
1. [Tutorial](https://javascript.info/)
2. [The JavaScript language](https://javascript.info/js)
3. [JavaScript Fundamentals](https://javascript.info/first-steps)
January 22, 2025
# Function expressions
In JavaScript, a function is not a âmagical language structureâ, but a special kind of value.
The syntax that we used before is called a *Function Declaration*:
```
```
There is another syntax for creating a function that is called a *Function Expression*.
It allows us to create a new function in the middle of any expression.
For example:
```
```
Here we can see a variable `sayHi` getting a value, the new function, created as `function() { alert("Hello"); }`.
As the function creation happens in the context of the assignment expression (to the right side of `=`), this is a *Function Expression*.
Please note, thereâs no name after the `function` keyword. Omitting a name is allowed for Function Expressions.
Here we immediately assign it to the variable, so the meaning of these code samples is the same: âcreate a function and put it into the variable `sayHi`â.
In more advanced situations, that weâll come across later, a function may be created and immediately called or scheduled for a later execution, not stored anywhere, thus remaining anonymous.
## [Function is a value](https://javascript.info/function-expressions#function-is-a-value)
Letâs reiterate: no matter how the function is created, a function is a value. Both examples above store a function in the `sayHi` variable.
We can even print out that value using `alert`:
```
```
Please note that the last line does not run the function, because there are no parentheses after `sayHi`. There are programming languages where any mention of a function name causes its execution, but JavaScript is not like that.
In JavaScript, a function is a value, so we can deal with it as a value. The code above shows its string representation, which is the source code.
Surely, a function is a special value, in the sense that we can call it like `sayHi()`.
But itâs still a value. So we can work with it like with other kinds of values.
We can copy a function to another variable:
```
```
Hereâs what happens above in detail:
1. The Function Declaration `(1)` creates the function and puts it into the variable named `sayHi`.
2. Line `(2)` copies it into the variable `func`. Please note again: there are no parentheses after `sayHi`. If there were, then `func = sayHi()` would write *the result of the call* `sayHi()` into `func`, not *the function* `sayHi` itself.
3. Now the function can be called as both `sayHi()` and `func()`.
We could also have used a Function Expression to declare `sayHi`, in the first line:
```
```
Everything would work the same.
Why is there a semicolon at the end?
You might wonder, why do Function Expressions have a semicolon `;` at the end, but Function Declarations do not:
```
```
The answer is simple: a Function Expression is created here as `function(âŠ) {âŠ}` inside the assignment statement: `let sayHi = âŠ;`. The semicolon `;` is recommended at the end of the statement, itâs not a part of the function syntax.
The semicolon would be there for a simpler assignment, such as `let sayHi = 5;`, and itâs also there for a function assignment.
## [Callback functions](https://javascript.info/function-expressions#callback-functions)
Letâs look at more examples of passing functions as values and using function expressions.
Weâll write a function `ask(question, yes, no)` with three parameters:
`question`
Text of the question
`yes`
Function to run if the answer is âYesâ
`no`
Function to run if the answer is âNoâ
The function should ask the `question` and, depending on the userâs answer, call `yes()` or `no()`:
```
```
In practice, such functions are quite useful. The major difference between a real-life `ask` and the example above is that real-life functions use more complex ways to interact with the user than a simple `confirm`. In the browser, such functions usually draw a nice-looking question window. But thatâs another story.
**The arguments `showOk` and `showCancel` of `ask` are called *callback functions* or just *callbacks*.**
The idea is that we pass a function and expect it to be âcalled backâ later if necessary. In our case, `showOk` becomes the callback for âyesâ answer, and `showCancel` for ânoâ answer.
We can use Function Expressions to write an equivalent, shorter function:
```
```
Here, functions are declared right inside the `ask(...)` call. They have no name, and so are called *anonymous*. Such functions are not accessible outside of `ask` (because they are not assigned to variables), but thatâs just what we want here.
Such code appears in our scripts very naturally, itâs in the spirit of JavaScript.
A function is a value representing an âactionâ
Regular values like strings or numbers represent the *data*.
A function can be perceived as an *action*.
We can pass it between variables and run when we want.
## [Function Expression vs Function Declaration](https://javascript.info/function-expressions#function-expression-vs-function-declaration)
Letâs formulate the key differences between Function Declarations and Expressions.
First, the syntax: how to differentiate between them in the code.
- *Function Declaration:* a function, declared as a separate statement, in the main code flow:
```
```
- *Function Expression:* a function, created inside an expression or inside another syntax construct. Here, the function is created on the right side of the âassignment expressionâ `=`:
```
```
The more subtle difference is *when* a function is created by the JavaScript engine.
**A Function Expression is created when the execution reaches it and is usable only from that moment.**
Once the execution flow passes to the right side of the assignment `let sum = functionâŠ` â here we go, the function is created and can be used (assigned, called, etc. ) from now on.
Function Declarations are different.
**A Function Declaration can be called earlier than it is defined.**
For example, a global Function Declaration is visible in the whole script, no matter where it is.
Thatâs due to internal algorithms. When JavaScript prepares to run the script, it first looks for global Function Declarations in it and creates the functions. We can think of it as an âinitialization stageâ.
And after all Function Declarations are processed, the code is executed. So it has access to these functions.
For example, this works:
```
```
The Function Declaration `sayHi` is created when JavaScript is preparing to start the script and is visible everywhere in it.
âŠIf it were a Function Expression, then it wouldnât work:
```
```
Function Expressions are created when the execution reaches them. That would happen only in the line `(*)`. Too late.
Another special feature of Function Declarations is their block scope.
**In strict mode, when a Function Declaration is within a code block, itâs visible everywhere inside that block. But not outside of it.**
For instance, letâs imagine that we need to declare a function `welcome()` depending on the `age` variable that we get during runtime. And then we plan to use it some time later.
If we use Function Declaration, it wonât work as intended:
```
```
Thatâs because a Function Declaration is only visible inside the code block in which it resides.
Hereâs another example:
```
```
What can we do to make `welcome` visible outside of `if`?
The correct approach would be to use a Function Expression and assign `welcome` to the variable that is declared outside of `if` and has the proper visibility.
This code works as intended:
```
```
Or we could simplify it even further using a question mark operator `?`:
```
```
When to choose Function Declaration versus Function Expression?
As a rule of thumb, when we need to declare a function, the first thing to consider is Function Declaration syntax. It gives more freedom in how to organize our code, because we can call such functions before they are declared.
Thatâs also better for readability, as itâs easier to look up `function f(âŠ) {âŠ}` in the code than `let f = function(âŠ) {âŠ};`. Function Declarations are more âeye-catchingâ.
âŠBut if a Function Declaration does not suit us for some reason, or we need a conditional declaration (weâve just seen an example), then Function Expression should be used.
## [Summary](https://javascript.info/function-expressions#summary)
- Functions are values. They can be assigned, copied or declared in any place of the code.
- If the function is declared as a separate statement in the main code flow, thatâs called a âFunction Declarationâ.
- If the function is created as a part of an expression, itâs called a âFunction Expressionâ.
- Function Declarations are processed before the code block is executed. They are visible everywhere in the block.
- Function Expressions are created when the execution flow reaches them.
In most cases when we need to declare a function, a Function Declaration is preferable, because it is visible prior to the declaration itself. That gives us more flexibility in code organization, and is usually more readable.
So we should use a Function Expression only when a Function Declaration is not fit for the task. Weâve seen a couple of examples of that in this chapter, and will see more in the future.
[Ctrl + âPrevious lesson](https://javascript.info/function-basics)[Ctrl + âNext lesson](https://javascript.info/arrow-functions-basics)
Share
[Tutorial map](https://javascript.info/tutorial/map)
## [Comments](https://javascript.info/function-expressions#comments)
read this before commentingâŠ
- If you have suggestions what to improve - please [submit a GitHub issue](https://github.com/javascript-tutorial/en.javascript.info/issues/new) or a pull request instead of commenting.
- If you can't understand something in the article â please elaborate.
- To insert few words of code, use the `<code>` tag, for several lines â wrap them in `<pre>` tag, for more than 10 lines â use a sandbox ([plnkr](https://plnkr.co/edit/?p=preview), [jsbin](https://jsbin.com/), [codepen](http://codepen.io/)âŠ)
#### Chapter
- [JavaScript Fundamentals](https://javascript.info/first-steps)
#### Lesson navigation
- [Function is a value](https://javascript.info/function-expressions#function-is-a-value)
- [Callback functions](https://javascript.info/function-expressions#callback-functions)
- [Function Expression vs Function Declaration](https://javascript.info/function-expressions#function-expression-vs-function-declaration)
- [Summary](https://javascript.info/function-expressions#summary)
- [Comments](https://javascript.info/function-expressions#comments)
Share
[Edit on GitHub](https://github.com/javascript-tutorial/en.javascript.info/blob/master/1-js/02-first-steps/16-function-expressions)
- © 2007â2026 Ilya Kantor
- [about the project](https://javascript.info/about)
- [contact us](https://javascript.info/about#contact-us)
- [terms of usage](https://javascript.info/terms)
- [privacy policy](https://javascript.info/privacy) |
| Readable Markdown | In JavaScript, a function is not a âmagical language structureâ, but a special kind of value.
The syntax that we used before is called a *Function Declaration*:
```
```
There is another syntax for creating a function that is called a *Function Expression*.
It allows us to create a new function in the middle of any expression.
For example:
```
```
Here we can see a variable `sayHi` getting a value, the new function, created as `function() { alert("Hello"); }`.
As the function creation happens in the context of the assignment expression (to the right side of `=`), this is a *Function Expression*.
Please note, thereâs no name after the `function` keyword. Omitting a name is allowed for Function Expressions.
Here we immediately assign it to the variable, so the meaning of these code samples is the same: âcreate a function and put it into the variable `sayHi`â.
In more advanced situations, that weâll come across later, a function may be created and immediately called or scheduled for a later execution, not stored anywhere, thus remaining anonymous.
## [Function is a value](https://javascript.info/function-expressions#function-is-a-value)
Letâs reiterate: no matter how the function is created, a function is a value. Both examples above store a function in the `sayHi` variable.
We can even print out that value using `alert`:
```
```
Please note that the last line does not run the function, because there are no parentheses after `sayHi`. There are programming languages where any mention of a function name causes its execution, but JavaScript is not like that.
In JavaScript, a function is a value, so we can deal with it as a value. The code above shows its string representation, which is the source code.
Surely, a function is a special value, in the sense that we can call it like `sayHi()`.
But itâs still a value. So we can work with it like with other kinds of values.
We can copy a function to another variable:
```
```
Hereâs what happens above in detail:
1. The Function Declaration `(1)` creates the function and puts it into the variable named `sayHi`.
2. Line `(2)` copies it into the variable `func`. Please note again: there are no parentheses after `sayHi`. If there were, then `func = sayHi()` would write *the result of the call* `sayHi()` into `func`, not *the function* `sayHi` itself.
3. Now the function can be called as both `sayHi()` and `func()`.
We could also have used a Function Expression to declare `sayHi`, in the first line:
```
```
Everything would work the same.
Why is there a semicolon at the end?
You might wonder, why do Function Expressions have a semicolon `;` at the end, but Function Declarations do not:
```
```
The answer is simple: a Function Expression is created here as `function(âŠ) {âŠ}` inside the assignment statement: `let sayHi = âŠ;`. The semicolon `;` is recommended at the end of the statement, itâs not a part of the function syntax.
The semicolon would be there for a simpler assignment, such as `let sayHi = 5;`, and itâs also there for a function assignment.
## [Callback functions](https://javascript.info/function-expressions#callback-functions)
Letâs look at more examples of passing functions as values and using function expressions.
Weâll write a function `ask(question, yes, no)` with three parameters:
`question`
Text of the question
`yes`
Function to run if the answer is âYesâ
`no`
Function to run if the answer is âNoâ
The function should ask the `question` and, depending on the userâs answer, call `yes()` or `no()`:
```
```
In practice, such functions are quite useful. The major difference between a real-life `ask` and the example above is that real-life functions use more complex ways to interact with the user than a simple `confirm`. In the browser, such functions usually draw a nice-looking question window. But thatâs another story.
**The arguments `showOk` and `showCancel` of `ask` are called *callback functions* or just *callbacks*.**
The idea is that we pass a function and expect it to be âcalled backâ later if necessary. In our case, `showOk` becomes the callback for âyesâ answer, and `showCancel` for ânoâ answer.
We can use Function Expressions to write an equivalent, shorter function:
```
```
Here, functions are declared right inside the `ask(...)` call. They have no name, and so are called *anonymous*. Such functions are not accessible outside of `ask` (because they are not assigned to variables), but thatâs just what we want here.
Such code appears in our scripts very naturally, itâs in the spirit of JavaScript.
A function is a value representing an âactionâ
Regular values like strings or numbers represent the *data*.
A function can be perceived as an *action*.
We can pass it between variables and run when we want.
## [Function Expression vs Function Declaration](https://javascript.info/function-expressions#function-expression-vs-function-declaration)
Letâs formulate the key differences between Function Declarations and Expressions.
First, the syntax: how to differentiate between them in the code.
- *Function Declaration:* a function, declared as a separate statement, in the main code flow:
```
```
- *Function Expression:* a function, created inside an expression or inside another syntax construct. Here, the function is created on the right side of the âassignment expressionâ `=`:
```
```
The more subtle difference is *when* a function is created by the JavaScript engine.
**A Function Expression is created when the execution reaches it and is usable only from that moment.**
Once the execution flow passes to the right side of the assignment `let sum = functionâŠ` â here we go, the function is created and can be used (assigned, called, etc. ) from now on.
Function Declarations are different.
**A Function Declaration can be called earlier than it is defined.**
For example, a global Function Declaration is visible in the whole script, no matter where it is.
Thatâs due to internal algorithms. When JavaScript prepares to run the script, it first looks for global Function Declarations in it and creates the functions. We can think of it as an âinitialization stageâ.
And after all Function Declarations are processed, the code is executed. So it has access to these functions.
For example, this works:
```
```
The Function Declaration `sayHi` is created when JavaScript is preparing to start the script and is visible everywhere in it.
âŠIf it were a Function Expression, then it wouldnât work:
```
```
Function Expressions are created when the execution reaches them. That would happen only in the line `(*)`. Too late.
Another special feature of Function Declarations is their block scope.
**In strict mode, when a Function Declaration is within a code block, itâs visible everywhere inside that block. But not outside of it.**
For instance, letâs imagine that we need to declare a function `welcome()` depending on the `age` variable that we get during runtime. And then we plan to use it some time later.
If we use Function Declaration, it wonât work as intended:
```
```
Thatâs because a Function Declaration is only visible inside the code block in which it resides.
Hereâs another example:
```
```
What can we do to make `welcome` visible outside of `if`?
The correct approach would be to use a Function Expression and assign `welcome` to the variable that is declared outside of `if` and has the proper visibility.
This code works as intended:
```
```
Or we could simplify it even further using a question mark operator `?`:
```
```
When to choose Function Declaration versus Function Expression?
As a rule of thumb, when we need to declare a function, the first thing to consider is Function Declaration syntax. It gives more freedom in how to organize our code, because we can call such functions before they are declared.
Thatâs also better for readability, as itâs easier to look up `function f(âŠ) {âŠ}` in the code than `let f = function(âŠ) {âŠ};`. Function Declarations are more âeye-catchingâ.
âŠBut if a Function Declaration does not suit us for some reason, or we need a conditional declaration (weâve just seen an example), then Function Expression should be used.
## [Summary](https://javascript.info/function-expressions#summary)
- Functions are values. They can be assigned, copied or declared in any place of the code.
- If the function is declared as a separate statement in the main code flow, thatâs called a âFunction Declarationâ.
- If the function is created as a part of an expression, itâs called a âFunction Expressionâ.
- Function Declarations are processed before the code block is executed. They are visible everywhere in the block.
- Function Expressions are created when the execution flow reaches them.
In most cases when we need to declare a function, a Function Declaration is preferable, because it is visible prior to the declaration itself. That gives us more flexibility in code organization, and is usually more readable.
So we should use a Function Expression only when a Function Declaration is not fit for the task. Weâve seen a couple of examples of that in this chapter, and will see more in the future. |
| Shard | 59 (laksa) |
| Root Hash | 11327735004675034659 |
| Unparsed URL | info,javascript!/function-expressions s443 |