ﺪﻫﺩ ﯽﻤﻧ ﻡﺎﺠﻧﺍ ﺍﺭ ﯽﺳﺭﺮﺑ ﭻﯿﻫ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺩﺮﮑﻠﻤﻋ<"code class="w3-codespan> ﮏﯾ
ﺪﻨﺷﺎﺑ ﻪﺘﺷﺍﺩ ﺪﻨﻧﺍﻮﺗ ﯽﻣ ﻊﺑﺍﻮﺗ ﻪﮐ ﺪﯾﺪﺷ ﻪﺟﻮﺘﻣ ﺵﺯﻮﻣﺁ ﻦﯾﺍ ﺭﺩ ﻼﺒﻗ
function functionName(parameter1, parameter2, parameter3) {
// code to be executed
}
ﺪﻨﺘﺴﻫ ﺭﺩ ﻩﺪﺷ ﺖﺳﺮﻬﻓ ﯼﺎﻫﻡﺎﻧ ﺎﻫﺮﺘﻣﺍﺭﺎﭘ ﻊﺑﺎﺗ
ﺪﻨﺘﺴﻫ ﯽﻌﻗﺍﻭ ﺮﯾﺩﺎﻘﻣArguments ﻊﺑﺎﺗ
ﺪﻨﮐ ﯽﻤﻧ ﺺﺨﺸﻣ ﻥﺁ ﯼﺍﺮﺑ ﺍﺭ ﻩﺩﺍﺩ ﻉﺍﻮﻧﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻊﺑﺎﺗ ﻒﯾﺭﺎﻌﺗ
ﺪﻨﻫﺩ ﯽﻤﻧ ﻡﺎﺠﻧﺍ ﺍﺭ ﻩﺪﺷ ﻝﺎﺳﺭﺍ ﺩﺭﻮﻣ ﺭﺩ ﻉﻮﻧ ﯽﺳﺭﺮﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻊﺑﺍﻮﺗ
.ﺪﻨﻨﮐ ﯽﻤﻧ ﯽﺳﺭﺮﺑ ﺍﺭ ﯽﺘﻓﺎﯾﺭﺩ ﯼﺎﻫ ﻥﺎﻣﻮﮔﺭﺁ ﺩﺍﺪﻌﺗ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻊﺑﺍﻮﺗ
ﯼﻭﺭ ﺮﺑ ﻪﺘﻓﺭ ﺖﺳﺩ ﺯﺍ ﺮﯾﺩﺎﻘﻣ ،ﺩﻮﺷ ﯽﻧﺍﻮﺧﺍﺮﻓ (ﻩﺪﺷ ﻡﻼﻋﺍ ﺯﺍ ﺮﺘﻤﮐ) ﻩﺪﺸﻤﮔ ﯼﺎﻫ ﻥﺎﻣﻮﮔﺁ<
ﺪﯿﻨﮐ ﻦﯿﯿﻌﺗ ﺽﺮﻓ ﺶﯿﭘ ﮏﯾ ﺖﺳﺍ ﺮﺘﻬﺑ ﺕﺎﻗﻭﺍ ﯽﻫﺎﮔ ﺎﻣﺍ ،ﺖﺳﺍ ﻝﻮﺒﻗ ﻞﺑﺎﻗ ﻦﯾﺍ ﺕﺎﻗﻭﺍ ﯽﻫﺎﮔ
function myFunction(x, y) {
if (y === undefined) {
y = 2;
}
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<p>Setting a default value to a function parameter.</p>
<p id="demo"></p>
<script>
function myFunction(x, y) {
if (y === undefined) {
y = 2;
}
return x * y;
}
document.getElementById("demo").innerHTML = myFunction(4);
</script>
</body>
</html>
ES6 ﺪﻨﺷﺎﺑ ﻪﺘﺷﺍﺩ ﺽﺮﻓ ﺶﯿﭘ ﺮﯾﺩﺎﻘﻣ ﺎﺗ ﺪﻫﺩ ﯽﻣ ﻩﺯﺎﺟﺍ ﻊﺑﺎﺗ ﯼﺎﻫﺮﺘﻣﺍﺭﺎﭘ ﻪﺑ.
.y=10 ،ﺪﺷﺎﺑ ﻩﺪﺸﻧ ﻒﯾﺮﻌﺗ ﺎﯾ ﻩﺪﺸﻧ ﺐﯾﻮﺼﺗ y ﺮﮔﺍ
function myFunction(x, y = 10) { return x + y;
}
myFunction(5);
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Functions</h1>
<h2>Default Parameter Values</h2>
<p>If y is not passed or undefined, then y = 10:</p>
<p id="demo"></p>
<script>
function myFunction(x, y = 10) {
return x + y;
}
document.getElementById("demo").innerHTML = myFunction(5);
</script>
</body>
</html>
:ﺩﺮﯿﮕﺑ ﺮﻈﻧ ﺭﺩ ﻪﯾﺍﺭﺁ ﮏﯾ ﻥﺍﻮﻨﻋ ﻪﺑ ﺍﺭ ﺎﻫ ﻥﺎﻣﻮﮔﺭﺁ ﺯﺍ ﯼﺩﻭﺪﺤﻣﺎﻧ ﺩﺍﺪﻌﺗ ﺎﺗ ﺪﻫﺩ ﯽﻣ ﻩﺯﺎﺟﺍ ﻊﺑﺎﺗ
function sum(...args) {
let sum = 0;
for (let arg of args) sum += arg;
return sum;
}
let x = sum(4, 9, 16, 25, 29, 100, 66, 77);
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Functions</h1>
<h2>The Rest Parameter</h2>
<p>The rest parameter (...) allows a function to treat an indefinite number of arguments as an array:</p>
<p id="demo"></p>
<script>
function sum(...args) {
let sum = 0;
for (let arg of args) sum += arg;
return sum;
}
let x = sum(4, 9, 16, 25, 29, 100, 66, 77);
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
ﺪﻨﺘﺴﻫ ﻥﺎﻣﻮﮔﺭﺁ ﻡﺎﻧ ﻪﺑ ﯽﻠﺧﺍﺩ ﯽﺷ ﮏﯾ ﯼﺍﺭﺍﺩ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻊﺑﺍﻮﺗ
ﺪﻧﻮﺷﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﻊﺑﺎﺗ ﻡﺎﮕﻨﻫ ﻪﮐ ﺖﺳﺍ ﯽﯾﺎﻫﻥﺎﻣﻮﮔﺭﺁ ﺯﺍ ﯼﺍﻪﯾﺍﺭﺁ ﯼﻭﺎﺣ ﻥﺎﻣﻮﮔﺭﺁ ء
ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﻦﯾﺮﺗﻻﺎﺑ (ﻼﺜﻣ) ﻥﺩﺮﮐ ﺍﺪﯿﭘ ﯼﺍﺮﺑ ﻊﺑﺎﺗ ﮏﯾ ﺯﺍ ﯽﮔﺩﺎﺳ ﻪﺑ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﺐﯿﺗﺮﺗ ﻦﯾﺍ ﻪﺑ
x = findMax(1, 123, 500, 115, 44, 88);
function findMax() {
let max = -Infinity;
for (let i = 0; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>Finding the largest number.</p>
<p id="demo"></p>
<script>
function findMax() {
let max = -Infinity;
for(let i = 0; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
}
document.getElementById("demo").innerHTML = findMax(4, 5, 6);
</script>
</body>
</html>
:ﺪﯿﻨﮐ ﺩﺎﺠﯾﺍ ﯼﺩﻭﺭﻭ ﺮﯾﺩﺎﻘﻣ ﻡﺎﻤﺗ ﻥﺩﺮﮐ ﻊﻤﺟ ﯼﺍﺮﺑ ﻊﺑﺎﺗ ﮏﯾ ﺎﯾ
x = sumAll(1, 123, 500, 115, 44, 88);
function sumAll() {
let sum = 0;
for (let i = 0; i < arguments.length; i++) {
sum += arguments[i];
}
return sum;
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Functions</h2>
<p>Sum of all arguments:</p>
<p id="demo"></p>
<script>
function sumAll() {
let sum = 0;
for(let i = 0; i < arguments.length; i++) {
sum += arguments[i];
}
return sum;
}
document.getElementById("demo").innerHTML = sumAll(1, 123, 500, 115, 44, 88);
</script>
</body>
</html>
.ﺩﻮﺷ ﯽﻧﺍﻮﺧﺍﺮﻓ (ﻩﺪﺷ ﻡﻼﻋﺍ ﺭﺍﺪﻘﻣ ﺯﺍ ﺶﯿﺑ) ﺪﺣ ﺯﺍ ﺶﯿﺑ ﯼﺎﻫﻥﺎﻣﻮﮔﺁ ﺎﺑ ﯽﻌ
.ﺪﻨﺘﺴﻫ ﻊﺑﺎﺗ ﯼﺎﻫ ﻥﺎﻣﻮﮔﺭﺁ ،ﻊﺑﺎﺗ ﯽﻧﺍﻮﺧﺍﺮﻓ ﺭﺩ ﺎﻫﺮﺘﻣﺍﺭﺎﭘ
ﻊﺑﺎﺗ ﻂﻘﻓ :ﺪﻧﻮﺷ ﯽﻣ ﻝﺎﺳﺭﺍ value ﻂﺳﻮﺗ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯼﺎﻫ ﻥﺎﻣﻮﮔﺭﺁ
ﺪﻫﺩ ﯽﻤﻧ ﺮﯿﯿﻐﺗ ﺍﺭ ﻥﺁ ﺮﺘﻣﺍﺭﺎﭘ ،ﺪﻫﺩ ﺮﯿﯿﻐﺗ ﺍﺭ ﻥﺎﻣﻮﮔﺭﺁ ﺭﺍﺪﻘﻣ ﯽﻌﺑﺎﺗ ﺮﮔﺍ
ﻥﺎﻣﻮﮔﺭﺁ ﺕﺍﺮﯿﯿﻐﺗﺪﻨﺘﺴﯿﻧ ﻩﺪﻫﺎﺸﻣ ﻞﺑﺎﻗ ﻊﺑﺎﺗ ﺯﺍ ﺝﺭﺎﺧ ﺭﺩ ﺎﻫ.
.ﺪﻨﺘﺴﻫ ﺮﯾﺩﺎﻘﻣ ﯽﺷ ﺕﺎﻋﺎﺟﺭﺍ ،ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺭﺩ
ﺪﻧﺍ ﻩﺪﺷ ﻝﺎﺳﺭﺍ :ﻊﺟﺮﻣ ﻂﺳﻮﺗ ﻪﮐ ﺩﺮﮐ ﺪﻨﻫﺍﻮﺧ ﺭﺎﺘﻓﺭ ﯼﺭﻮﻃ ﺎﯿﺷﺍ ،ﻞﯿﻟﺩ ﻦﯿﻤﻫ ﻪﺑ
.ﺪﻫﺩ ﯽﻣ ﺮﯿﯿﻐﺗ ﺍﺭ ﯽﻠﺻﺍ ﺭﺍﺪﻘﻣ ،ﺪﻫﺩ ﺮﯿﯿﻐﺗ ﺍﺭ ﯽﺷ ﯽﮔﮋﯾﻭ ﮏﯾ ﻊﺑﺎﺗ ﮏﯾ ﺮﮔﺍ
(ﺩﻮﺷ ﯽﻣ ﺲﮑﻌﻨﻣ) ﺖﺳﺍ ﻩﺪﻫﺎﺸﻣ ﻞﺑﺎﻗ ﻊﺑﺎﺗ ﺯﺍ ﺝﺭﺎﺧ ءﯽﺷ ﺕﺎﯿﺻﻮﺼﺧ ﺭﺩ ﺕﺍﺮﯿﯿﻐﺗ.