ﺪﺷ ﯽﻓﺮﻌﻣ let
ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ
ﺪﻨﺘﺴﯿﻧ ﺩﺪﺠﻣ ﻥﻼﻋﺍ ﻞﺑﺎﻗ let
ﺎﺑ ﻩﺪﺷ ﻒﯾﺮﻌﺗ ﯼﺎﻫﺮﯿﻐﺘﻣ
ﺪﻧﻮﺷ ﻡﻼﻋﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺯﺍ ﻞﺒﻗ ﺪﯾﺎﺑ let
ﺎﺑ ﻩﺪﺷ ﻒﯾﺮﻌﺗ ﯼﺎﻫﺮﯿﻐﺘﻣ
ﺪﻨﺷﺎﺑ Block Scope ﯼﺍﺭﺍﺩ let
ﺎﺑ ﻩﺪﺷ ﻒﯾﺮﻌﺗ ﯼﺎﻫﺮﯿﻐﺘﻣ
.ﺩﺮﮐ ﻡﻼﻋﺍ ﺍًﺩﺪﺠﻣ ﻥﺍﻮﺗ ﯽﻤﻧlet
ﺎﺑ ﻩﺪﺷ ﻒﯾﺮﻌﺗ ﯼﺎﻫﺮﯿﻐﺘﻣ
.ﺪﯿﻨﮐ ﻡﻼﻋﺍ ﺍًﺩﺪﺠﻣ ﺍﺭ let
ﺎﺑ ﻩﺪﺷﻡﻼﻋﺍ ﺮﯿﻐﺘﻣ ﮏﯾ ﯽﻓﺩﺎﺼﺗ ﺭﻮﻃﻪﺑ ﺪﯿﻧﺍﻮﺗﯽﻤﻧ
:ﺪﯿﻫﺩ ﻡﺎﺠﻧﺍ ﺍﺭ ﺭﺎﮐ ﻦﯾﺍ ﺪﯾﺎﺒﻧ، ﺪﯿﻫﺩ ﻩﺯﺎﺟﺍ<"code class="w3-codespan> ﺎﺑ
let x = "John Doe";
let x = 0;
:ﺪﯿﻧﺍﻮﺗ ﯽﻣ var
ﺎﺑ
var x = "John Doe";
var x = 0;
.ﺩﻮﺑ Function Scope ﻭ Global Scope ﯼﺍﺭﺍﺩ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ،ES6 (2015) ﺯﺍ ﻞﺒﻗ
ES6 ﺩﺮﮐ ﯽﻓﺮﻌﻣ ﺍﺭ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻢﻬﻣ ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ ﻭﺩ: let
ﻭ con
.ﺪﻨﻨﮐ ﯽﻣ ﻪﺋﺍﺭﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺭﺩ ﺍﺭ Block Scope ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ ﻭﺩ ﻦﯾﺍ
ﺪﻨﺘﺴﯿﻧ ﯽﺳﺮﺘﺳﺩ ﻞﺑﺎﻗ { } ﮎﻮﻠﺑ ﮏﯾ ﺭﺩ ﻩﺪﺷ ﻡﻼﻋﺍ ﯼﺎﻫﺮﯿﻐﺘﻣ
{
let x = 2;
}
// x can NOT be used here
.ﺪﻨﺷﺎﺑ ﻪﺘﺷﺍﺩ ﯽﮐﻮﻠﺑ ﻩﺩﻭﺪﺤﻣ ﺪﻨﻧﺍﻮﺗ ﯽﻤﻧ var
ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ ﺎﺑ ﻩﺪﺷ ﻡﻼﻋﺍ ﯼﺎﻫﺮﯿﻐﺘﻣ
ﺖﺷﺍﺩ ﯽﺳﺮﺘﺳﺩ ﻥﺁ ﺯﺍ ﻥﺍﻮﺗ ﯽﻣ ﺍﺭ { } ﮎﻮﻠﺑ ﮏﯾ ﻞﺧﺍﺩ ﺭﺩ ﻩﺪﺷ ﻡﻼﻋﺍ ﯼﺎﻫﺮﯿﻐﺘﻣ
{
var x = 2;
}
// x CAN be used here
.ﺪﻨﮐ ﺩﺎﺠﯾﺍ ﯽﺗﻼﮑﺸﻣ ﺪﻧﺍﻮﺗ ﯽﻣ var
ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ ﺮﯿﻐﺘﻣ ﮏﯾ ﺩﺪﺠﻣ ﻡﻼﻋﺍ
ﺪﻨﮐ ﯽﻣ ﻡﻼﻋﺍ ﺍًﺩﺪﺠﻣ ﺍﺭ ﺮﯿﻐﺘﻣ ﺰﯿﻧ ﮎﻮﻠﺑ ﮏﯾ ﻞﺧﺍﺩ ﺭﺩ ﺮﯿﻐﺘﻣ ﮏﯾ ﺩﺪﺠﻣ ﻡﻼﻋﺍ
var x = 10;
// Here x is 10
{
var x = 2;
// Here x is 2
}
// Here x is 2
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>Redeclaring a Variable Using var</h2>
<p id="demo"></p>
<script>
var x = 10;
// Here x is 10
{
var x = 2;
// Here x is 2
}
// Here x is 2
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
.ﺪﻨﮐ ﻞﺣ ﺍﺭ ﻞﮑﺸﻣ ﻦﯾﺍ ﺪﻧﺍﻮﺗ ﯽﻣ let
ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ ﺮﯿﻐﺘﻣ ﮏﯾ ﺩﺪﺠﻣ ﻡﻼﻋﺍ
ﺪﻨﮐ ﯽﻤﻧ ﻡﻼﻋﺍ ﺍًﺩﺪﺠﻣ ﺍﺭ ﻥﺁ ﺯﺍ ﺝﺭﺎﺧ ﺮﯿﻐﺘﻣ ،ﮎﻮﻠﺑ ﮏﯾ ﻞﺧﺍﺩ ﺭﺩ ﺮﯿﻐﺘﻣ ﮏﯾ ﺩﺪﺠﻣ ﻡﻼﻋﺍ
let x = 10;
// Here x is 10
{
let x = 2;
// Here x is 2
}
// Here x is 10
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>Redeclaring a Variable Using let</h2>
<p id="demo"></p>
<script>
let x = 10;
// Here x is 10
{
let x = 2;
// Here x is 2
}
// Here x is 10
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
Scope | Redeclare | Reassign | Hoisted | Binds this | |
var | No | Yes | Yes | Yes | Yes |
let | Yes | No | Yes | No | No |
const | Yes | No | No | No | No |
ﺪﯿﻫﺩ ﻩﺯﺎﺟﺍ
ﻭ const
ﺪﯿﻫﺩ ﻩﺯﺎﺟﺍ
ﻭ const
ﺪﯿﻫﺩ ﻩﺯﺎﺟﺍ
ﻭ const
ﺪﯿﻫﺩ ﻩﺯﺎﺟﺍ
ﻭ const
ﺪﯿﻫﺩ ﻩﺯﺎﺟﺍ
ﻭ const
var
ﺩﻮﺷ ﻡﻼﻋﺍ ﺖﺴﯿﻧ ﻡﺯﻻ.
var
ﺖﺳﺍ ﻪﺘﻓﺎﯾ ﺶﯾﺍﺰﻓﺍ.
var
ﺖﺳﺍ ﻞﺼﺘﻣ ﻦﯾﺍ ﻪﺑ.
ﺪﻨﺘﺴﻫ const
ﻭ let
ﯼﺪﯿﻠﮐ ﺕﺎﻤﻠﮐ
:ﺪﻨﮐ ﯽﻣ ﻒﯾﺮﻌﺗ ﺍﺭ ﻞﻣﺎﮐ ﯽﻧﺎﺒﯿﺘﺸﭘ ﺎﺑ ﺮﮔﺭﻭﺮﻣ ﯼﺎﻫ ﻪﺨﺴﻧ ﻦﯿﻟﻭﺍ ﺮﯾﺯ ﻝﻭﺪﺟ
Chrome 49 | Edge 12 | Firefox 36 | Safari 11 | Opera 36 |
Mar, 2016 | Jul, 2015 | Jan, 2015 | Sep, 2017 | Mar, 2016 |
ﺖﺳﺍ ﺯﺎﺠﻣ var
ﺎﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺮﯿﻐﺘﻣ ﺩﺪﺠﻣ ﻡﻼﻋﺍ
var x = 2;
// Now x is 2
var x = 3;
// Now x is 3
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript let</h2>
<p>Redeclaring a JavaScript variable with <b>var</b> is allowed anywhere in a program:</p>
<p id="demo"></p>
<script>
var x = 2;
// Now x is 2
var x = 3;
// Now x is 3
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
:ﺖﺴﯿﻧ ﺯﺎﺠﻣ ﮎﻮﻠﺑ ﻥﺎﻤﻫ ﺭﺩ ﺮﯿﻐﺘﻣ ﮏﯾ ﺩﺪﺠﻣ ﻡﻼﻋﺍ ،let
ﺎﺑ
var x = 2; // Allowed
let x = 3; // Not allowed
{
let x = 2; // Allowed
let x = 3; // Not allowed
}
{
let x = 2; // Allowed
var x = 3; // Not allowed
}
:ﺖﺳﺍ ﺯﺎﺠﻣ ،ﺮﮕﯾﺩ ﮎﻮﻠﺑ ﮏﯾ ﺭﺩ ،let
ﺎﺑ ﺮﯿﻐﺘﻣ ﮏﯾ ﺩﺪﺠﻣ ﻡﻼﻋﺍ
let x = 2; // Allowed
{
let x = 3; // Allowed
}
{
let x = 4; // Allowed
}
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript let</h2>
<p>Redeclaring a variable with <b>let</b>, in another scope, or in another block, is allowed:</p>
<p id="demo"></p>
<script>
let x = 2; // Allowed
{
let x = 3; // Allowed
}
{
let x = 4; // Allowed
}
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
ﺪﻨﺘﺴﻫ ﻻﺎﺑ ﻻﺎﺑ ﺭﺩ var
ﺎﺑ ﻩﺪﺷ ﻒﯾﺮﻌﺗ ﯼﺎﻫﺮﯿﻐﺘﻣ
:ﺖﺳﺍ ﺏﻮﺧ ﻦﯾﺍ
carName = "Volvo";
var carName;
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Hoisting</h2>
<p>With <b>var</b>, you can use a variable before it is declared:</p>
<p id="demo"></p>
<script>
carName = "Volvo";
document.getElementById("demo").innerHTML = carName;
var carName;
</script>
</body>
</html>
.ﺪﯿﻨﮐ ﻪﻌﻟﺎﻄﻣ ﺍﺭ Hoisting ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻞﺼﻓ ،ﺪﯿﻧﺍﺪﺑ ﺮﺘﺸﯿﺑ ﻥﺩﺮﺑ ﻻﺎﺑ ﺩﺭﻮﻣ ﺭﺩ ﺪﯿﻫﺍﻮﺧ ﯽﻣ ﺮﮔﺍ
ﮏﯾ ﻪﺑ ﺮﺠﻨﻣ ﻥﻼﻋﺍ ﺯﺍ ﻞﺒﻗ let
ﺮﯿﻐﺘﻣ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ :ﯽﻨﻌﻣ
carName = "Saab";
let carName = "Volvo";
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Hoisting</h2>
<p>With <b>let</b>, you cannot use a variable before it is declared.</p>
<p id="demo"></p>
<script>
try {
carName = "Saab";
let carName = "Volvo";
}
catch(err) {
document.getElementById("demo").innerHTML = err;
}
</script>
</body>
</html>