ﺪﺷ ﯽﻓﺮﻌﻣ const
ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ
ﺩﺮﮐ ﻡﻼﻋﺍ ﻩﺭﺎﺑﻭﺩ ﻥﺍﻮﺗﯽﻤﻧ ﺍﺭ const
ﺎﺑ ﻩﺪﺷ ﻒﯾﺮﻌﺗ ﯼﺎﻫﺮﯿﻐﺘﻣ
ﺪﻨﺘﺴﯿﻧ ﺩﺪﺠﻣ ﺺﯿﺼﺨﺗ ﻞﺑﺎﻗ const
ﺎﺑ ﻩﺪﺷ ﻒﯾﺮﻌﺗ ﯼﺎﻫﺮﯿﻐﺘﻣ
ﺪﻨﺘﺴﻫ Block Scope ﯼﺍﺭﺍﺩ const
ﺎﺑ ﻩﺪﺷ ﻒﯾﺮﻌﺗ ﯼﺎﻫﺮﯿﻐﺘﻣ
:ﺩﺍﺩ ﺹﺎﺼﺘﺧﺍ ﻩﺭﺎﺑﻭﺩ ﻥﺍﻮﺗ ﯽﻤﻧ ﺍﺭ const
ﺮﯿﻐﺘﻣ ﮏﯾ
const PI = 3.141592653589793;
PI = 3.14; // This will give an error
PI = PI + 10; // This will also give an error
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript const</h2>
<p id="demo"></p>
<script>
try {
const PI = 3.141592653589793;
PI = 3.14;
}
catch (err) {
document.getElementById("demo").innerHTML = err;
}
</script>
</body>
</html>
:ﺩﻮﺷ ﻩﺩﺍﺩ ﺹﺎﺼﺘﺧﺍ ﺎﻬﻧﺁ ﻪﺑ ﺭﺍﺪﻘﻣ ﮏﯾ ﺪﻧﻮﺷﯽﻣ ﻥﻼﻋﺍ ﻪﮐ ﯽﻧﺎﻣﺯ ﺪﯾﺎﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ const
const PI = 3.14159265359;
const PI;
PI = 3.14159265359;
ﺎﺑ ﺍﺭ ﺮﯿﻐﺘﻣ ﮏﯾ ﻪﺸﯿﻤﻫ const
ﻪﮐ ﺪﯿﻧﺍﺩ ﯽﻣ ﯽﺘﻗﻭ ﺪﯿﻨﮐ ﻡﻼﻋﺍ
:ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ const
ﺯﺍ ﺪﯿﻨﮐ ﯽﻣ ﻡﻼﻋﺍ ﯽﺘﻗﻭ
ﺪﯾﺪﺟ ﻪﯾﺍﺭﺁ ﮏﯾ
ﺪﯾﺪﺟ ﯽﺷ ﮏﯾ
ﺪﯾﺪﺟ ﺩﺮﮑﻠﻤﻋ ﮏﯾ
ﺪﯾﺪﺟ RegExp ﮏﯾ
.ﺖﺳﺍ ﻩﺪﻨﻨﮐ ﻩﺍﺮﻤﮔ ﯽﻤﮐ 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>
:ﺪﯿﻫﺩ ﺹﺎﺼﺘﺧﺍ ﻩﺭﺎﺑﻭﺩ ﺍﺭ ﻪﯾﺍﺭﺁ ﺪﯿﻧﺍﻮﺗ ﯽﻤﻧ ﺎﻤﺷ ﺎﻣﺍ
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>
:ﺪﯿﻫﺩ ﺮﯿﯿﻐﺗ ﺍﺭ ﺖﺑﺎﺛ ﯽﺷ ﮏﯾ ﯼﺎﻫ ﯽﮔﮋﯾﻭ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﺎﻤﺷ
// You can create a const object:
const car = {type:"Fiat", model:"500", color:"white"};
// You can change a property:
car.color = "red";
// You can add a property:
car.owner = "Johnson";
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript const</h2>
<p>Declaring a constant object does NOT make the objects properties unchangeable:</p>
<p id="demo"></p>
<script>
// Create an object:
const car = {type:"Fiat", model:"500", color:"white"};
// Change a property:
car.color = "red";
// Add a property:
car.owner = "Johnson";
// Display the property:
document.getElementById("demo").innerHTML = "Car owner is " + car.owner;
</script>
</body>
</html>
:ﺪﯿﻫﺩ ﺹﺎﺼﺘﺧﺍ ﻩﺭﺎﺑﻭﺩ ﺍﺭ ﯽﺷ ﺪﯿﻧﺍﻮﺗ ﯽﻤﻧ ﺎﻤﺷ ﺎﻣﺍ
const car = {type:"Fiat", model:"500", color:"white"};
car = {type:"Volvo", model:"EX60", color:"red"}; //
ERROR
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript const</h2>
<p>You can NOT reassign a constant object:</p>
<p id="demo"></p>
<script>
try {
const car = {type:"Fiat", model:"500", color:"white"};
car = {type:"Volvo", model:"EX60", color:"red"};
}
catch (err) {
document.getElementById("demo").innerHTML = err;
}
</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 |
.ﺖﺳﺍ let
ﻪﺑﺎﺸﻣ const
ﺎﺑ ﺮﯿﻐﺘﻣ ﮏﯾ ﻥﻼﻋﺍ
x ﺎﺑ ،ﻝﺎﺜﻣ ﻦﯾﺍ ﺭﺩ ،ﮎﻮﻠﺑ ﺭﺩ ﻩﺪﺷ ﻡﻼﻋﺍ x ﺖﺴﯿﻧ ﻥﺎﺴﮑﯾ ﮎﻮﻠﺑ ﺯﺍ ﺝﺭﺎﺧ ﺭﺩ ﻩﺪﺷ ﻡﻼﻋﺍ:
const x = 10;
// Here x is 10
{
const x = 2;
// Here x is 2
}
// Here x is 10
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScropt const variables has block scope</h2>
<p id="demo"></p>
<script>
const x = 10;
// Here x is 10
{
const x = 2;
// Here x is 2
}
// Here x is 10
document.getElementById("demo").innerHTML = "x is " + x;
</script>
</body>
</html>
.ﺪﯿﻨﮐ ﺐﺴﮐ ﯼﺮﺘﺸﯿﺑ ﺕﺎﻋﻼﻃﺍ ﮎﻮﻠﺑ ﻩﺩﻭﺪﺤﻣ ﻩﺭﺎﺑﺭﺩ ﺪﯿﻧﺍﻮﺗ ﯽﻣ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺶﺨﺑ ﺭﺩ
ﺖﺳﺍ ﺯﺎﺠﻣ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ var
ﺮﯿﻐﺘﻣ ﺩﺪﺠﻣ ﻡﻼﻋﺍ
var x = 2; // Allowed
var x = 3; // Allowed
x = 4; // Allowed
ﺩﻮﺟﻮﻣ ﻩﺯﺎﺟﺍ<"code class="w3-codespan> ﺎﯾ
var
ﺩﺪﺠﻣ ﻡﻼﻋﺍ
var x = 2; // Allowed
const x = 2; // Not allowed
{
let x = 2; // Allowed
const x = 2; // Not allowed
}
{
const x = 2; // Allowed
const x = 2; // Not allowed
}
:ﺖﺴﯿﻧ ﺯﺎﺠﻣ ،ﻩﺩﻭﺪﺤﻣ ﻥﺎﻤﻫ ﺭﺩ ،ﺩﻮﺟﻮﻣ const
ﺮﯿﻐﺘﻣ ﮏﯾ ﺩﺪﺠﻣ ﺺﯿﺼﺨﺗ
const x = 2; // Allowed
x = 2; // Not allowed
var x = 2; // Not allowed
let x = 2; // Not allowed
const x = 2; // Not allowed
{ const x = 2; // Allowed
x = 2;
// Not allowed
var x = 2;
// Not allowed
let x = 2;
// Not allowed
const x = 2; // Not allowed
}
:ﺖﺳﺍ ﺯﺎﺠﻣ ،ﺮﮕﯾﺩ ﮎﻮﻠﺑ ﺭﺩ ﺎﯾ ،ﺮﮕﯾﺩ ﻩﺩﻭﺪﺤﻣ ﺭﺩ ،const
ﺎﺑ ﺮﯿﻐﺘﻣ ﮏﯾ ﺩﺪﺠﻣ ﻡﻼﻋﺍ
const x = 2; // Allowed
{ const x = 3; // Allowed
}
{
const x = 4; // Allowed
}
ﺪﻨﺘﺴﻫ ﻻﺎﺑ ﻻﺎﺑ ﺭﺩ 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 ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻞﺼﻓ ،ﺪﯿﻧﺍﺪﺑ ﺮﺘﺸﯿﺑ ﻥﺩﺮﺑ ﻻﺎﺑ ﺩﺭﻮﻣ ﺭﺩ ﺪﯿﻫﺍﻮﺧ ﯽﻣ ﺮﮔﺍ
ﮏﯾ ﻪﺑ ﺮﺠﻨﻣ ﻥﻼﻋﺍ ﺯﺍ ﻞﺒﻗ const
ﺮﯿﻐﺘﻣ ﮏﯾ ﺯﺍ ﻩﺩﺎﻔﺘﺳﺍ :ﯽﻨﻌﻣ
alert (carName);
const carName = "Volvo";
→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Hoisting</h2>
<p>With <b>const</b>, you cannot use a variable before it is declared:</p>
<p id="demo"></p>
<script>
try {
alert(carName);
const carName = "Volvo";
}
catch (err) {
document.getElementById("demo").innerHTML = err;
}
</script>
</body>
</html>