ﺪﻧﻮﺷ ﯽﻣ ﻒﯾﺮﻌﺗ ﺎﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻊﺑﺍﻮﺗ
ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ a ﺎﯾ ﻥﻼﻋﺍ ﻊﺑﺎﺗ ﮏﯾ ﺯﺍ ﺪﯿﻧﺍﻮﺗ ﯽﻣ
ﺪﻧﻮﺷ ﯽﻣ ﻥﻼﻋﺍ ﺎﺑ ﻊﺑﺍﻮﺗ ﻪﮐ ﺪﯿﺘﻓﺮﮔ ﺩﺎﯾ ،ﺵﺯﻮﻣﺁ ﻦﯾﺍ ﺭﺩ ﻼًﺒﻗ
function functionName(parameters) {
// code to be executed
}
"ﺪﻧﻮﺷ ﯽﻣ ﻩﺮﯿﺧﺫ ﯼﺪﻌﺑ ﻩﺩﺎﻔﺘﺳﺍ ﯼﺍﺮﺑ" ﺎﻬﻧﺁ .ﺪﻧﻮﺷ ﯽﻤﻧ ﺍﺮﺟﺍ ﻪﻠﺻﺎﻓﻼﺑ ﻩﺪﺷ ﻡﻼﻋﺍ ﻊﺑﺍﻮﺗ
function myFunction(a, b) {
return a * b;
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Functions</h1>
<p>Call a function which performs a calculation and returns the result:</p>
<p id="demo"></p>
<script>
let x = myFunction(4, 3);
document.getElementById("demo").innerHTML = x;
function myFunction(a, b) {
return a * b;
}
</script>
</body>
</html>
.ﺩﻮﺷ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﯾﺍﺮﺟﺍ ﺕﺍﺭﻮﺘﺳﺩ ﯼﺯﺎﺳﺍﺪﺟ ﯼﺍﺮﺑ ﻝﻮﮔﺮﯾﻭ ﻪﻄﻘﻧ
.ﺩﻮﺷ ﻒﯾﺮﻌﺗ ﺕﺭﺎﺒﻋ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ ﺪﻧﺍﻮﺗ ﯽﻣ ﻦﯿﻨﭽﻤﻫ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻊﺑﺎﺗ ﮏﯾ
:ﺩﺮﮐ ﻩﺮﯿﺧﺫ ﺮﯿﻐﺘﻣ ﮏﯾ ﺭﺩ ﻥﺍﻮﺗ ﯽﻣ ﺍﺭ ﻊﺑﺎﺗ ﺕﺭﺎﺒﻋ ﮏﯾ
const x = function (a, b) {return a * b};
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>A function can be stored in a variable:</p>
<p id="demo"></p>
<script>
const x = function (a, b) {return a * b};
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
ﺪﻧﺍﻮﺗ ﯽﻣ ﺮﯿﻐﺘﻣ ،ﺪﺷ ﻩﺮﯿﺧﺫ ﺮﯿﻐﺘﻣ ﮏﯾ ﺭﺩ ﻊﺑﺎﺗ ﺕﺭﺎﺒﻋ ﮏﯾ ﻪﮑﻨﯾﺍ ﺯﺍ ﺪﻌﺑ
const x = function (a, b) {return a * b};
let z = x(4, 3);
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>After a function has been stored in a variable,
the variable can be used as a function:</p>
<p id="demo"></p>
<script>
const x = function (a, b) {return a * b};
document.getElementById("demo").innerHTML = x(4, 3);
</script>
</body>
</html>
a ﻥﻭﺪﺑ ﻊﺑﺎﺗ ﮏﯾ) ﺖﺳﺍ ﺱﺎﻨﺷﺎﻧ ﻊﺑﺎﺗ ﮏﯾ ﻊﻗﺍﻭ ﺭﺩ ﻻﺎﺑ ﻊﺑﺎﺗ
ﺪﻨﺘﺴﻫ ﻪﺸﯿﻤﻫ ﺎﻬﻧﺁ .ﺪﻧﺭﺍﺪﻧ ﻊﺑﺍﻮﺗ ﻡﺎﻧ ﻪﺑ ﯼﺯﺎﯿﻧ ﺎﻫﺮﯿﻐﺘﻣ ﺭﺩ ﻩﺪﺷ ﻩﺮﯿﺧﺫ ﻊﺑﺍﻮﺗ
.ﺖﺳﺍ ﯽﯾﺍﺮﺟﺍ ﺭﻮﺘﺳﺩ ﮏﯾ ﺯﺍ ﯽﺸﺨﺑ ﺍﺮﯾﺯ ﺪﺳﺭ ﯽﻣ ﻥﺎﯾﺎﭘ ﻪﺑ ﻝﻮﮔﺮﯾﻭ ﻪﻄﻘﻧ ﺎﺑ ﻻﺎﺑ ﻊﺑﺎﺗ
ﺪﻧﺍ ﻩﺪﺷ ﻒﯾﺮﻌﺗ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻊﺑﺍﻮﺗ ،ﺪﯾﺩﺮﮐ ﻩﺪﻫﺎﺸﻣ ﯽﻠﺒﻗ ﯼﺎﻫ ﻝﺎﺜﻣ ﺭﺩ ﻪﮐ ﺭﻮﻄﻧﺎﻤﻫ
ﺩﺮﮐ ﻒﯾﺮﻌﺗ ﻡﺎﻧ ﻪﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻊﺑﺍﻮﺗ ﻩﺪﻧﺯﺎﺳ ﺎﺑ ﻥﺍﻮﺗ ﯽﻣ ﻦﯿﻨﭽﻤﻫ ﺍﺭ ﻊﺑﺍﻮﺗ
const myFunction = new Function("a", "b", "return a * b");
let x = myFunction(4, 3);
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>JavaScript has an built-in function constructor.</p>
<p id="demo"></p>
<script>
const myFunction = new Function("a", "b", "return a * b");
document.getElementById("demo").innerHTML = myFunction(4, 3);
</script>
</body>
</html>
:ﺖﺳﺍ ﻦﺘﺷﻮﻧ ﺪﻨﻧﺎﻣ ﻻﺎﺑ ﻝﺎﺜﻣ .ﺪﯾﺭﺍﺪﻧ ﻊﺑﺎﺗ ﻩﺪﻧﺯﺎﺳ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﻪﺑ ﯼﺯﺎﯿﻧ ﻊﻗﺍﻭ ﺭﺩ ﺎﻤﺷ
const myFunction = function (a, b) {return a * b};
let x = myFunction(4, 3);
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p id="demo"></p>
<script>
const myFunction = function (a, b) {return a * b}
document.getElementById("demo").innerHTML = myFunction(4, 3);
</script>
</body>
</html>
.ﺪﯿﻨﮐ ﺏﺎﻨﺘﺟﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺭﺩ new
ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ ﺯﺍ ﻩﺩﺎﻔﺘ
.ﺪﯾﺪﺷ ﺎﻨﺷﺁ (Hoisting ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ) "ﻥﺩﺮﺑ ﻻﺎﺑ" ﺎﺑ ﺵﺯﻮﻣﺁ ﻦﯾﺍ ﺭﺩ ﻼًﺒﻗ
Hoisting ﺶﯿﭘ ﺭﺎﺘﻓﺭﻝﺎﻘﺘﻧﺍ ﯼﺍﺮﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺽﺮﻓ ﻪﯿﻣﻼﻋﺍﺎﻫ ﺖﺳ
.ﺩﻮﺷ ﯽﻣ ﻝﺎﻤﻋﺍ ﻊﺑﺎﺗ ﯼﺎﻫ ﻥﻼﻋﺍ ﯼﺍﺮﺑ ﻭ ﺮﯿﻐﺘﻣ ﯼﺎﻫ ﻥﻼﻋﺍ ﯼﺍﺮﺑ ﻥﺩﺮﺑ ﻻﺎﺑ
:ﺩﺮﮐ ﯽﻧﺍﻮﺧﺍﺮﻓ ﻡﻼﻋﺍ ﺯﺍ ﻞﺒﻗ ﻥﺍﻮﺗ ﯽﻣ ﺍﺭ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻊﺑﺍﻮﺗ ،ﻞﯿﻟﺩ ﻦﯿﻤﻫ ﻪﺑ
myFunction(5);
function myFunction(y) {
return y * y;
}
.ﺪﻧﻭﺭ ﯽﻤﻧ ﻻﺎﺑ ﺕﺭﺎﺒﻋ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ ﻩﺪﺷ ﻒﯾﺮﻌﺗ ﻊﺑﺍﻮﺗ
.ﺖﺧﺎﺳ "ﯽﻧﺍﻮﺧﺍﺮﻓ ﺩﻮﺧ" ﻥﺍﻮﺗ ﯽﻣ ﺍﺭ ﻊﺑﺎﺗ ﺕﺍﺭﺎﺒﻋ
.(ﺩﻮﺷ ﯽﻣ ﻉﻭﺮﺷ) ﺩﻮﺷ ﯽﻣ ﯽﻧﺍﻮﺧﺍﺮﻓ ﯽﻧﺍﻮﺧﺍﺮﻓ ﻥﻭﺪﺑ ،ﺭﺎﮐﺩﻮﺧ ﺭﻮﻃ ﻪﺑ self-invoking ﺕﺭﺎﺒﻋ ﮏﯾ
ﺪﻧﻮﺷ ﯽﻣ ﺍﺮﺟﺍ ﺭﺎﮐﺩﻮﺧ ﺭﻮﻃ ﻪﺑ ﻊﺑﺎﺗ ﺕﺍﺭﺎﺒﻋ ،ﺩﻮﺷ ﻝﺎﺒﻧﺩ ﺕﺭﺎﺒﻋ ﺮﮔﺍ
.ﺪﯿﻨﮐ ﯽﻧﺍﻮﺧﺍﺮﻓ ﺩﻮﺧ ﺍﺭ ﻊﺑﺎﺗ ﻥﻼﻋﺍ ﮏﯾ ﺪﯿﻧﺍﻮﺗ ﯽﻤﻧ ﺎﻤﺷ
ﺪﯿﻨﮐ ﻪﻓﺎﺿﺍ ﺪﯾﺎﺑ
(function () {
let x = "Hello!!"; // I will invoke myself
})();
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<p>Functions can be invoked automatically without being called:</p>
<p id="demo"></p>
<script>
(function () {
document.getElementById("demo").innerHTML = "Hello! I called myself";
})();
</script>
</body>
</html>
ﻊﺑﺎﺗ) ﺱﺎﻨﺷﺎﻧ ﯽﻧﺍﻮﺧﺍﺮﻓ ﺩﻮﺧ ﺩﺮﮑﻠﻤﻋ ﮏﯾ ﻊﻗﺍﻭ ﺭﺩ ﻕﻮﻓ ﻊﺑﺎﺗ
:ﺩﺮﮐ ﻩﺩﺎﻔﺘﺳﺍ ﺮﯾﺩﺎﻘﻣ ﻥﺍﻮﻨﻋ ﻪﺑ ﻥﺍﻮﺗ ﯽﻣ ﺍﺭ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻊﺑﺍﻮﺗ
function myFunction(a, b) {
return a * b;
}
let x = myFunction(4, 3);
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>Functions can be treated as values:</p>
<p>x = myFunction(4,3) or x = 12</p>
<p>In both cases, x becomes a number with the value of 12.</p>
<p id="demo"></p>
<script>
function myFunction(a, b) {
return a * b;
}
let x = myFunction(4, 3);
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
:ﺩﺮﮐ ﻩﺩﺎﻔﺘﺳﺍ ﺕﺍﺭﺎﺒﻋ ﺭﺩ ﻥﺍﻮﺗ ﯽﻣ ﺍﺭ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻊﺑﺍﻮﺗ
function myFunction(a, b) {
return a * b;
}
let x = myFunction(4, 3) * 2;
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>Functions can be used in expressions.</p>
<p id="demo"></p>
<script>
function myFunction(a, b) {
return a * b;
}
let x = myFunction(4, 3) * 2;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
ﯼﺍﺮﺑ ﺍﺭ "ﻊﺑﺎﺗ" ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺭﺩ ﻉﻮﻧ<"code class="w3-codespan> ﺮﮕﻠﻤﻋ
.ﺩﺮﮐ ﻒﯿﺻﻮﺗ ءﺎﯿﺷﺍ ﻥﺍﻮﻨﻋ ﻪﺑ ﻞﮑﺷ ﻦﯾﺮﺘﻬﺑ ﻪﺑ ﻥﺍﻮﺗ ﯽﻣ ﺍﺭ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻊﺑﺍﻮﺗ ،ﺎﻣﺍ
ﺪﻨﺘﺴﻫ ﻭ ﺹﺍﻮﺧ ﯼﺍﺭﺍﺩ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻊﺑﺍﻮﺗ
.ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﺍﺭ ﯽﺘﻓﺎﯾﺭﺩ ﯼﺎﻫ ﻥﺎﻣﻮﮔﺭﺁ ﺩﺍﺪﻌﺗ arguments.length<
function myFunction(a, b) {
return arguments.length;
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<p>The arguments.length property returns the number of arguments received by the function:</p>
<p id="demo"></p>
<script>
function myFunction(a, b) {
return arguments.length;
}
document.getElementById("demo").innerHTML = myFunction(4, 3);
</script>
</body>
</html>
:ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﻪﺘﺷﺭ ﺕﺭﻮﺻ ﻪﺑ ﺍﺭ ﻊﺑﺎﺗ toString()
ﺪﺘﻣ
function myFunction(a, b) {
return a * b;
}
let text = myFunction.toString();
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Functions</h1>
<h2>The toString() Method</h2>
<p>The toString() method returns the function as a string:</p>
<p id="demo"></p>
<script>
function myFunction(a, b) {
return a * b;
}
document.getElementById("demo").innerHTML = myFunction.toString();
</script>
</body>
</html>
.ﺩﻮﺷ ﯽﻣ ﻩﺪﯿﻣﺎﻧ ﯽﺷ ﯼﺍﺮﺑ ﺪﺘﻣ ﮏﯾ ،ﺩﻮﺷ ﯽﻣ ﻒﯾﺮﻌﺗ ﯽﺷ ﮏﯾ ﯽﮔﮋﯾﻭ ﻥﺍﻮﻨﻋ ﻪﺑ ﻪﮐ ﯽﻌﺑﺎﺗ
.ﺪﻫﺩ ﯽﻣ ﻩﺯﺎﺟﺍ ﻊﺑﺎﺗ ﺕﺍﺭﺎﺒﻋ ﻦﺘﺷﻮﻧ ﯼﺍﺮﺑ ﻩﺎﺗﻮﮐ ﻮﺤﻧ ﮏﯾ ﻥﺎﮑﯿﭘ ﻊﺑﺍﻮﺗ
ﻭ return
ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ ،funct
// ES5
var x = function(x, y) {
return x * y;
}
// ES6
const x = (x, y) => x * y;
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arrow Functions</h2>
<p>With arrow functions, you don't have to type the function keyword, the return keyword, and the curly brackets.</p>
<p>Arrow functions are not supported in IE11 or earlier.</p>
<p id="demo"></p>
<script>
const x = (x, y) => x * y;
document.getElementById("demo").innerHTML = x(5, 5);
</script>
</body>
</html>
.ﺪﻧﺭﺍﺪﻧ ﺍﺭ ﺩﻮﺧ ﻦﯾﺍ<"code class="w3-codespan> ﻥﺎﮑﯿﭘ ﻊﺑﺍﻮﺗ
.ﺪﻧﻮﺷ ﻒﯾﺮﻌﺗ ﻩﺩﺎﻔﺘﺳﺍ ﺯﺍ ﻞﺒﻗ ﺪﯾﺎﺑ ﺎﻬﻧﺁ .ﺪﻧﻮﺷ ﯽﻤﻧ ﺪﻨﻠﺑ ﻥﺎﮑﯿﭘ ﻊﺑﺍﻮﺗ
const
ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ
.ﺪﺷﺎﺑ ﺪﺣﺍﻭ ﺕﺭﺎﺒﻋ ﮏﯾ ﻊﺑﺎﺗ ﻪﮐ ﺪﯿﻨﮐ ﻑﺬﺣ ﺍﺭ ﯼﺮﻓﺮﻓ ﯼﺎﻫﺖﮐﺍﺮﺑ ﻭ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ ﻝﺎﺜﻣ
const x = (x, y) => { return x * y };
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Arrow Functions</h2>
<p>Arrow functions are not supported in IE11 or earlier.</p>
<p id="demo"></p>
<script>
const x = (x, y) => { return x * y };
document.getElementById("demo").innerHTML = x(5, 5);
</script>
</body>
</html>
.ﺪﻧﻮﺷ ﯽﻤﻧ ﯽﻧﺎﺒﯿﺘﺸﭘ ﻥﺁ ﺯﺍ ﻞﺒﻗ ﺎﯾ IE11 ﺭﺩ ﻥﺎﮑﯿﭘ ﻊﺑﺍﻮﺗ