.const
:ﺩﺮﮐ ﯽﻓﺮﻌﻣ ﺍﺭ ﯼﺪﯾﺪﺟ ﻢﻬﻣ ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭ
:ﺖﺳﺍ ﻩﺪﺷ ﻞﯾﺪﺒﺗ ﻝﻮﻤﻌﻣ ﺵﻭﺭ ﮏﯾ ﻪﺑ const
ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ ﺎﺑ ﺎ
const cars = ["Saab", "Volvo", "BMW"];
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript const</h2>
<p id="demo"></p>
<script>
const cars = ["Saab", "Volvo", "BMW"];
document.getElementById("demo").innerHTML = cars;
</script>
</body>
</html>
:ﺖﺴﯿﻧ ﺩﺪﺠﻣ ﺺﯿﺼﺨﺗ ﻞﺑﺎﻗ const
ﺎﺑ ﻩﺪﺷ ﻡﻼﻋﺍ ﻪﯾﺍﺭﺁ ﮏﯾ
const cars = ["Saab", "Volvo", "BMW"];
cars = ["Toyota", "Volvo", "Audi"]; // ERROR
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript const</h2>
<p>You can NOT reassign a constant array:</p>
<p id="demo"></p>
<script>
try {
const cars = ["Saab", "Volvo", "BMW"];
cars = ["Toyota", "Volvo", "Audi"];
}
catch (err) {
document.getElementById("demo").innerHTML = err;
}
</script>
</body>
</html>
.ﺖﺳﺍ ﻩﺪﻨﻨﮐ ﻩﺍﺮﻤﮔ ﯽﻤﮐ const
ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ
.ﺪﻨﮐ ﯽﻣ ﻒﯾﺮﻌﺗ ﺍﺭ ﻪﯾﺍﺭﺁ ﮏﯾ ﻪﺑ ﺖﺑﺎﺛ ﻉﺎﺟﺭﺍ ﮏﯾ .ﺪﻨﮐ ﯽﻤﻧ ﻒﯾﺮﻌﺗ ﺖﺑﺎﺛ ﻪﯾﺍﺭﺁ ﮏﯾ
.ﻢﯿﻫﺩ ﺮﯿﯿﻐﺗ ﺍﺭ ﺖﺑﺎﺛ ﻪﯾﺍﺭﺁ ﮏﯾ ﺮﺻﺎﻨﻋ ﻢﯿﻧﺍﻮﺗ ﯽﻣ ﻢﻫ ﺯﻮﻨﻫ ﺎﻣ ،ﻞﯿﻟﺩ ﻦﯿﻤﻫ ﻪﺑ
:ﺪﯿﻫﺩ ﺮﯿﯿﻐﺗ ﺍﺭ ﺖﺑﺎﺛ ﻪﯾﺍﺭﺁ ﮏﯾ ﺮﺻﺎﻨﻋ ﺪﯿﻧﺍﻮﺗ ﯽﻣ
// You can create a constant array:
const cars = ["Saab", "Volvo", "BMW"];
// You can change an element:
cars[0] = "Toyota";
// You can add an element:
cars.push("Audi");
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript const</h2>
<p>Declaring a constant array does NOT make the elements unchangeable:</p>
<p id="demo"></p>
<script>
// Create an Array:
const cars = ["Saab", "Volvo", "BMW"];
// Change an element:
cars[0] = "Toyota";
// Add an element:
cars.push("Audi");
// Display the Array:
document.getElementById("demo").innerHTML = cars;
</script>
</body>
</html>
.ﺩﻮﺷ ﯽﻤﻧ ﯽﻧﺎﺒﯿﺘﺸﭘ ﻥﺁ ﺯﺍ ﻞﺒﻗ ﺎﯾ 10 ﺭﺭﻮﻠﭙﺴﮐﺍ ﺖﻧﺮﺘﻨﯾﺍ ﺭﺩ const
:ﺪﻨﮐ ﯽﻣ ﻒﯾﺮﻌﺗ const
ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ ﺯﺍ ﻞﻣﺎﮐ ﯽﻧﺎﺒﯿﺘﺸﭘ ﺎﺑ ﺍ
Chrome 49 | IE 11 / Edge | Firefox 36 | Safari 10 | Opera 36 |
Mar, 2016 | Oct, 2013 | Feb, 2015 | Sep, 2016 | Mar, 2016 |
:ﺩﻮﺷ ﻩﺩﺍﺩ ﺹﺎﺼﺘﺧﺍ ﺎﻬﻧﺁ ﻪﺑ ﺭﺍﺪﻘﻣ ﮏﯾ ﺪﻧﻮﺷﯽﻣ ﻥﻼﻋﺍ ﻪﮐ ﯽﻧﺎﻣﺯ ﺪﯾﺎﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺖﺳﺍ ﻮﺤﻧ ﮏﯾ ﻪﯾﺍﺭﺁ ﻪﯿﻟﻭﺍ ﯽﻫﺩﺭﺍﺪﻘﻣ ﻥﻭﺪﺑ :ﺩﺮﮐ ﺪﻫﺍﻮﺨﻧ ﺭﺎﮐ ﻦﯾﺍ .ﺩﺮﮐ ﻪﯿﻟﻭﺍ ﯽﻫﺩﺭﺍﺪﻘﻣ ﻥﺎﻣﺯ ﺮﻫ ﺭﺩ ﻥﺍﻮﺗ ﯽﻣ ﺍﺭ :ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﻡﻼﻋﺍ ﺯﺍ ﻞﺒﻗ ﻪﯾﺍﺭﺁ ﺯﺍ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﯽﺘﺣ :ﺖﺳﺍ ﺏﻮﺧ ﻦﯾﺍ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺖﺳﺍ Block Scope ﯼﺍﺭﺍﺩ :ﺖﺴﯿﻧ ﻥﺎﺴﮑﯾ ﺖﺳﺍ ﻩﺪﺷ ﻡﻼﻋﺍ ﮎﻮﻠﺑ ﺯﺍ ﺝﺭﺎﺧ ﻪﮐ ﯼﺍ ﻪﯾﺍﺭﺁ ﺎﺑ ﮎﻮﻠﺑ ﮏﯾ ﺭﺩ ﻩﺪﺷ ﻡﻼﻋﺍ ﻪﯾﺍﺭﺁ ﮏﯾ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ :ﺩﺭﺍﺪﻧ ﮎﻮﻠﺑ ﻩﺩﻭﺪﺤﻣ ،ﺖﺳﺍ ﻩﺪﺷ ﻡﻼﻋﺍ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺪﯿﻨﮐ ﺐﺴﮐ ﯼﺮﺘﺸﯿﺑ ﺕﺎﻋﻼﻃﺍ JavaScript Scope :ﺶﺨﺑ ﺭﺩ Block Scope ﺩﺭﻮﻣ ﺭﺩ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﺎﻤﺷ :ﺖﺳﺍ ﺯﺎﺠﻣ ﻪﻣﺎﻧﺮﺑ ﺯﺍ ﯽﯾﺎﺟ ﺮﻫ ﺭﺩ ﺖﺳﺍ ﻩﺪﺷ ﻡﻼﻋﺍ ﺭﺩ ﺎﯾ ﻩﺩﻭﺪﺤﻣ ﻥﺎﻤﻫ ﺭﺩ ، ﺭﺩ ﺎﯾ ﻩﺩﻭﺪﺤﻣ ﻥﺎﻤﻫ ﺭﺩ ،ﺩﻮﺟﻮﻣ :ﺖﺳﺍ ﺯﺎﺠﻣ ﺮﮕﯾﺩ ﮎﻮﻠﺑ ﺭﺩ ﺎﯾ ﺮﮕﯾﺩ ﻩﺩﻭﺪﺤﻣ ﺭﺩ ، :ﺪﯾﻭﺮﺑ ﺎﻣ ﺱﺭﺩﺁ ﻪﺑ ،ﻪﯾﺍﺭﺁ ﻞﻣﺎﮐ ﻊﺟﺮﻣ ﮏﯾ ﯼﺍﺮﺑ .ﻞﻣﺎﮐ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻪﯾﺍﺭﺁ ﻊﺟﺮﻣ ﺖﺳﺍ ﺎﻫ ﻪﯾﺍﺭﺁ ﻪﻤﻫ ﺯﺍ ﯽﯾﺎﻫ ﻪﻧﻮﻤﻧ ﻭ ﺕﺎﺤﯿﺿﻮﺗ ﯼﻭﺎﺣ ﻊﺟﺮﻣconst
ﺯﺍ ﻩﺩﺎﻔﺘﺳ ﻝﺎﺜﻣ
const cars;
cars = ["Saab", "Volvo", "BMW"];
var
ﺎﺑ ﻩﺪﺷ ﻝﺎﺜﻣ
cars = ["Saab", "Volvo", "BMW"];
var cars;
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>JavaScript Hoisting</h2>
<p id="demo"></p>
<script>
cars = ["Saab", "Volvo", "BMW"];
var cars;
document.getElementById("demo").innerHTML = cars[0];
</script>
</body>
</html>
Const Block Scope
const
ﺎﺑ ﻩﺪﺷ ﻡﻼﻋﺍ ﻪﯾﺍﺭ ﻝﺎﺜﻣ
const cars = ["Saab", "Volvo", "BMW"];
// Here cars[0] is "Saab"
{
const cars = ["Toyota", "Volvo", "BMW"];
// Here cars[0] is "Toyota"
}
// Here cars[0] is "Saab"
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>Declaring an Array Using const</h2>
<p id="demo"></p>
<script>
const cars = ["Saab", "Volvo", "BMW"];
// Here cars[0] is "Saab"
{
const cars = ["Toyota", "Volvo", "BMW"];
// Here cars[0] is "Toyota"
}
// Here cars[0] is "Saab"
document.getElementById("demo").innerHTML = cars[0];
</script>
</body>
</html>
var
ﺎﺑ ﻪﮐ ﯼﺍ ﻪﯾﺍﺭﺁ ﻝﺎﺜﻣ
var cars = ["Saab", "Volvo", "BMW"];
// Here cars[0] is "Saab"
{
var cars = ["Toyota", "Volvo", "BMW"];
// Here cars[0] is "Toyota"
}
// Here cars[0] is "Toyota"
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>Declaring an Array Using var</h2>
<p id="demo"></p>
<script>
var cars = ["Saab", "Volvo", "BMW"];
// Here cars[0] is "Saab"
{
var cars = ["Toyota", "Volvo", "BMW"];
// Here cars[0] is "Toyota"
}
// Here cars[0] is "Toyota"
document.getElementById("demo").innerHTML = cars[0];
</script>
</body>
</html>
ﺎﻫ ﻪﯾﺍﺭﺁ ﺩﺪﺠﻣ ﻡﻼﻋﺍ
var
ﺎﺑ ﻪ ﻝﺎﺜﻣ
var cars = ["Volvo", "BMW"]; // Allowed
var cars = ["Toyota", "BMW"]; // Allowed
cars = ["Volvo", "Saab"]; // Allowed
const
ﻪﺑ ﻪﯾﺍﺭﺁ ﮏﯾ ﺩﺪﺠﻣ ﺺﯿﺼﺨﺗ ﺎ ﻝﺎﺜﻣ
var cars = ["Volvo", "BMW"]; // Allowed
const cars = ["Volvo", "BMW"]; // Not allowed
{
var cars = ["Volvo", "BMW"]; // Allowed
const cars = ["Volvo", "BMW"]; // Not allowed
}
const
ﻪﯾﺍﺭﺁ ﮏﯾ ﺩﺪﺠﻣ ﺺﯿﺼﺨ ﻝﺎﺜﻣ
const cars = ["Volvo", "BMW"]; // Allowed
const cars = ["Volvo", "BMW"]; // Not allowed
var cars = ["Volvo", "BMW"]; // Not allowed
cars = ["Volvo", "BMW"]; // Not allowed
{
const cars = ["Volvo", "BMW"]; // Allowed
const cars = ["Volvo", "BMW"]; // Not allowed
var cars = ["Volvo", "BMW"]; // Not allowed
cars = ["Volvo", "BMW"]; // Not allowed
}
const
ﺎﺑ ﯼ ﻝﺎﺜﻣ
const cars = ["Volvo", "BMW"]; // Allowed
{
const cars = ["Volvo", "BMW"]; // Allowed
}
{
const cars = ["Volvo", "BMW"]; // Allowed
}
ﻪﯾﺍﺭﺁ ﻞﻣﺎﮐ ﻊﺟﺮﻣ