ℹ️ 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.3 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://www.includehelp.com/code-snippets/var-functionname-function-vs-function-functionname.aspx |
| Last Crawled | 2026-04-08 11:30:48 (8 days ago) |
| First Indexed | 2022-05-21 02:26:19 (3 years ago) |
| HTTP Status Code | 200 |
| Meta Title | var functionName = function() {} vs function functionName() {} |
| Meta Description | Here, you are going to understand the two different ways of writing a function in JavaScript, var functionName = function() {} or function functionName() {}. |
| Meta Canonical | null |
| Boilerpipe Text | Home
»
JavaScript Examples
Learn, what is the difference between var functionName = function() {} and function functionName() {}.
Submitted by
Pratishtha Saxena
, on May 20, 2022
A function is composed of a sequence of statements called the
function body
. Values can be
passed
to a function, and the function will
return
a value.
Now, there are broadly two ways of writing a function in the JavaScript code.
1) var functionName = function() {}
This is a function expression. It is only defined when that line is reached. This function can only be called after that point in the code. This is because the function is assigned to the variable
functionName
at run time.
Syntax:
var functionName = function() {...};
Example 1:
var
functionOne
=
function
(
)
{
console
.
log
(
"
Hello World!
"
)
;
}
;
functionOne
(
)
;
Output:
Hello World!
Since, when we define a function in this way, then it is only defined from that line onwards. If the function is called before declaring then it will return error.
Example 2:
// TypeError: functionOne is not a function
functionOne
(
)
;
var
functionOne
=
function
(
)
{
console
.
log
(
"
Hello World!
"
)
;
}
;
Output:
Uncaught TypeError: functionOne is not a function
at <anonymous>:2:1
2) function functionName() {}
This is a function declaration and is defined as quickly as its surrounding characteristic or script is executed (due to hoisting). Here, the function is available to code that runs above where the function is declared. The function is assigned to that identifier,
functionName
, at parse time.
Syntax:
function functionName() {…};
Example 3:
functionTwo
(
)
;
function
functionTwo
(
)
{
console
.
log
(
"
Hello World!
"
)
;
}
Output:
Hello World!
JavaScript Examples »
Advertisement
Advertisement |
| Markdown | null |
| Readable Markdown | [Home](https://www.includehelp.com/) » [JavaScript Examples](https://www.includehelp.com/code-snippets/javascript-examples.aspx)
Learn, what is the difference between var functionName = function() {} and function functionName() {}.
Submitted by [Pratishtha Saxena](https://www.includehelp.com/Members/Pratishtha-Saxena.aspx), on May 20, 2022
A function is composed of a sequence of statements called the ***function body***. Values can be ***passed*** to a function, and the function will ***return*** a value.
Now, there are broadly two ways of writing a function in the JavaScript code.
## 1\) var functionName = function() {}
This is a function expression. It is only defined when that line is reached. This function can only be called after that point in the code. This is because the function is assigned to the variable *functionName* at run time.
**Syntax:**
```
var functionName = function() {...};
```
**Example 1:**
```
var functionOne = function() {
console.log("Hello World!");
};
functionOne();
```
**Output:**
```
Hello World!
```
Since, when we define a function in this way, then it is only defined from that line onwards. If the function is called before declaring then it will return error.
**Example 2:**
```
// TypeError: functionOne is not a function
functionOne();
var functionOne = function() {
console.log("Hello World!");
};
```
**Output:**
```
Uncaught TypeError: functionOne is not a function
at <anonymous>:2:1
```
## 2\) function functionName() {}
This is a function declaration and is defined as quickly as its surrounding characteristic or script is executed (due to hoisting). Here, the function is available to code that runs above where the function is declared. The function is assigned to that identifier, *functionName*, at parse time.
**Syntax:**
```
function functionName() {…};
```
**Example 3:**
```
functionTwo();
function functionTwo() {
console.log("Hello World!");
}
```
**Output:**
```
Hello World!
```
[JavaScript Examples »](https://www.includehelp.com/code-snippets/javascript-examples.aspx)
***
Advertisement
Advertisement |
| Shard | 106 (laksa) |
| Root Hash | 1184066710662109106 |
| Unparsed URL | com,includehelp!www,/code-snippets/var-functionname-function-vs-function-functionname.aspx s443 |