ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺕﺎﻫﺎﺒﺘﺷﺍ


فهرست مطالب

    نمایش فهرست مطالب


.ﺪﻨﮐ ﯽﻣ ﻩﺭﺎﺷﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺞﯾﺍﺭ ﺕﺎﻫﺎﺒﺘﺷﺍ ﺯﺍ ﯽﺧﺮﺑ ﻪﺑ ﻞﺼﻓ ﻦﯾﺍ


ﺺﯿﺼﺨﺗ ﺭﻮﺗﺍﺮﭘﺍ ﺯﺍ ﯽﻓﺩﺎﺼﺗ ﻩﺩﺎﻔﺘﺳﺍ

ﺪﻨﻨﮐ ﺩﺎﺠﯾﺍ ﺲﯾﻮﻧﻪﻣﺎﻧﺮﺑ ﺩﻮﺟﻭ ﺕﺭﻮﺻ ﺭﺩ ﺍﺭ ﯼﺍﻩﺮﻈﺘﻨﻣﺮﯿﻏ ﺞﯾﺎﺘﻧ ﺖﺳﺍ ﻦﮑﻤﻣ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭ

ﻥﺍﻮﻨﻋ ﻪﺑ) ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﺍﺭ ﺖﺳﺭﺩﺎﻧ<"code class="w3-codespan">if

let x = 0;
if (x == 10) 

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Comparisons</h2>

<p>This returns false (as expected) because x is not equal to 10:</p>

<p id="demo"></p>

<script>
let x = 0;
document.getElementById("demo").innerHTML = Boolean(x == 10);
</script>

</body>
</html>

ﻪﻧ ﺪﯾﺎﺷ) ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﺍﺭ ﺖﺳﺭﺩ<"code class="w3-codespan">if

let x = 0;
if (x = 10)

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Common JavaScript Mistakes</h2>

<p>This returns true (maybe not as expected), because 10 is true:</p>

<p id="demo"></p>

<script>
let x = 0;
document.getElementById("demo").innerHTML = Boolean(x = 10);
</script>

</body>
</html>

ﻪﻧ ﺪﯾﺎﺷ) ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﺍﺭ ﺖﺳﺭﺩﺎﻧ<"code class="w3-codespan">if

let x = 0;
if (x = 0)

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Common JavaScript Mistakes</h2>

<p>This if statement returns false (maybe not as expected), because 0 is false:</p>

<p id="demo"></p>

<script>
let x = 0;
document.getElementById("demo").innerHTML = Boolean(x = 0);
</script>

</body>
</html>

.ﺪﻧﺍﺩﺮﮔ ﯽﻣﺮﺑ ﺍﺭ ﺏﺎﺴﺘﻧﺍ ﺭﺍﺪﻘﻣ ﻪﺸﯿﻤﻫ ﺏﺎﺴﺘﻧﺍ ﮏﯾ


ﺖﺴﺳ ﻪﺴﯾﺎﻘﻣ ﺭﺎﻈﺘﻧﺍ

ﺩﺩﺮﮔ ﯽﻣﺮﺑ if ﺕﺭﺎﺒﻋ ﻦﯾﺍ .ﺖﺴﯿﻧ ﻢﻬﻣ ﻩﺩﺍﺩ ﻉﻮﻧ ،ﯽﻟﻮﻤﻌﻣ ﻪﺴ

let x = 10;
let y = "10";
if (x == y) 

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Common JavaScript Mistakes</h2>

<p>In regular comparison, data type does not matter. This if statement returns true:</p>

<p id="demo"></p>

<script>
let x = 10;
let y = "10";
document.getElementById("demo").innerHTML = Boolean(x == y);
</script>

</body>
</html>

:ﺪﻧﺍﺩﺮﮔﯽﻣﺮﺑ ﺍﺭ code class="w3-codespan">if false> ﺕﺭﺎﺒﻋ ﻦﯾﺍ .ﺖﺳﺍ ﻢﻬﻣ ﻩﺩﺍﺩ

let x = 10;
let y = "10";
if (x === y) 

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Common JavaScript Mistakes</h2>

<p>In strict comparison, data type does matter. This if statement returns false:</p>

<p id="demo"></p>

<script>
let x = 10;
let y = "10";
document.getElementById("demo").innerHTML = Boolean(x === y);
</script>

</body>
</html>

.ﺩﻮﺷ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﻪﻧﺍﺮﯿﮕﺘﺨﺳ switch ﺕﺍﺭﻮﺘﺳﺩ ﺭﺩ ﻢﯿﻨﮐ ﺵﻮﻣ

:ﺪﻫﺩ ﯽﻣ ﺶﯾﺎﻤﻧ ﺭﺍﺪﺸﻫ ﮏﯾ ﺩﺭﻮﻣ ﭻﯿﺋﻮﺳ<"code class="w3-codespan> ﻦﯾﺍ

let x = 10;
switch(x) {
  case 10: alert("Hello");
    } 

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Common JavaScript Mistakes</h2>

<p>It is a common mistake to forget that switch statements use strict comparison.</p>
<p>This will work:</p>

<p id="demo"></p>

<script>
let x = 10;
switch(x) {
  case 10: document.getElementById("demo").innerHTML = "Hello";
}
</script>

</body>
</html>

:ﺪﻫﺩ ﯽﻤﻧ ﺶﯾﺎﻤﻧ ﺍﺭ ﯼﺭﺍﺪﺸﻫ ﺩﺭﻮﻣ ﭻﯿﺋﻮﺳ<"code class="w3-codespan> ﻦﯾﺍ

let x = 10;
switch(x) {
  case "10": alert("Hello");
    } 

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Common JavaScript Mistakes</h2>

<p>It is a common mistake to forget that switch statements use strict comparison.</p>
<p>This will not work:</p>

<p id="demo"></p>

<script>
let x = 10;
switch(x) {
  case "10": document.getElementById("demo").innerHTML = "Hello";
}
</script>

</body>
</html>


ﻩﺪﻨﻨﮐ ﺞﯿﮔ ﻕﺎﺤﻟﺍ ﻭ ﻥﺩﻭﺰﻓﺍ

ﻥﺩﻭﺰﻓﺍ ﻥﺩﻭﺰﻓﺍ ﺩﺭﻮﻣ ﺭﺩ ﺩﺍﺪﻋﺍ ﺖﺳﺍ.

Concatenation ﻥﺩﻭﺰﻓﺍ ﺩﺭﻮﻣ ﺭﺩ ﻪﺘﺷﺭ ﺖﺳﺍ.

.ﺪﻨﻨﮐ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ + ﺮﮕﻠﻤﻋ ﮏﯾ ﺯﺍ ﺕﺎﯿﻠﻤﻋ ﻭﺩ ﺮﻫ ﺖﭙﯾﺮﮑﺳﺍ

ﺩﻮﺑ ﺪﻫﺍﻮﺧ ﺕﻭﺎﻔﺘﻣ ﺩﺪﻋ ﮏﯾ ﻥﺍﻮﻨﻋ ﻪﺑ ﺩﺪﻋ ﮏﯾ ﻥﺩﺮﮐ ﻪﻓﺎﺿﺍ ،ﻞﯿﻟﺩ ﻦﯿﻤﻫ ﻪﺑ

let x = 10;
x = 10 + 5;       // 
    Now x is 15

let y = 10;
y += "5";        
    // Now y is "105"

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Common JavaScript Mistakes</h2>

<p>Adding a number as a number produces a different result from adding a number as a string:</p>

<p id="demo"></p>

<script>
let y = 10
y += "5";
document.getElementById("demo").innerHTML = y;
</script>

</body>
</html>

:ﺪﺷﺎﺑ ﺭﺍﻮﺷﺩ ﺖﺳﺍ ﻦﮑﻤﻣ ﻪﺠﯿﺘﻧ ﯽﻨﯿﺑ ﺶﯿﭘ ،ﺮﯿﻐﺘﻣ ﻭﺩ ﻥﺩﺮﮐ ﻪﻓﺎﺿﺍ ﻡﺎﮕﻨﻫ

let x = 10;
let y = 5;
let z = x + y;     // Now z is 15

let x = 10;
let y = "5";
let z = x + y;     // Now z is "105"

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Common JavaScript Mistakes</h2>

<p>Adding a number as a number produces a different result from adding a number as a string:</p>

<p id="demo"></p>

<script>
let x = 10;
let y = "5";
let z = x + y;
document.getElementById("demo").innerHTML = z;
</script>

</body>
</html>

ﺎﻫﺭﻭﺎﻨﺷ ﻢﻫﺎﻔﺗﻮﺳ

ﺪﻧﻮﺷ ﯽﻣ ﻩﺮﯿﺧﺫ ﯽﺘﯿﺑ 64/>ﺭﻭﺎﻨﺷ ﺰﯿﻤﻣ ﺩﺍﺪﻋﺍ ﻥﺍﻮﻨﻋ ﻪﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺭﺩ ﺩﺍﺪﻋ

ﺪﻧﺭﺍﺩ ﻞﮑﺸﻣ ﻥﺁ ﺎﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻪﻠﻤﺟ ﺯﺍ ﯽﺴﯾﻮﻧ ﻪﻣﺎﻧﺮﺑ ﯼﺎﻫ ﻥﺎﺑﺯ ﻪﻤﻫ

let x = 0.1;
let y = 0.2;
let z = x + y            
    // the result in z will not be 0.3

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Common JavaScript Mistakes</h2>

<p>All programming languages, including JavaScript, have difficulties with precise floating point values:</p>

<p id="demo"></p>

<script>
let x = 0.1;
let y = 0.2;
let z = x + y;
document.getElementById("demo").innerHTML = z;
</script>

</body>
</html>

:ﺪﻨﮐ ﯽﻣ ﮏﻤﮐ ﻢﯿﺴﻘﺗ ﻭ ﺏﺮﺿ ﻪﺑ ،ﻻﺎﺑ ﻞﮑﺸﻣ ﻞﺣ ﯼﺍﺮﺑ

ﻝﺎﺜﻣ

let z = (x * 10 + y * 10) / 10;       // z will be 0.3

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Common JavaScript Mistakes</h2>

<p>All programming languages, including JavaScript, have difficulties with precise floating point values.</p>
<p>To solve the problem, it helps to multiply and divide:</p>

<p id="demo"></p>

<script>
let x = 0.1;
let y = 0.2;
let z = (x * 10 + y *10) / 10;
document.getElementById("demo").innerHTML = z;
</script>

</body>
</html>


ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﻪﺘﺷﺭ ﻦﺘﺴﮑﺷ

:ﺪﯿﻨﮐ ﻢﯿﺴﻘﺗ ﻂﺧ ﻭﺩ ﻪﺑ ﺍﺭ ﺕﺭﺎﺒﻋ ﮏﯾ ﻪﮐ ﺪﻫﺩ ﯽﻣ ﺍﺭ ﻥﺎﮑﻣﺍ ﻦﯾﺍ ﺎﻤﺷ ﻪﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ

1 ﻝﺎﺜﻣ

let x =
"Hello World!";

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Breaking a JavaScript Statement</h2>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML =
"Hello World!";
</script>

</body>
</html>

:ﺩﺮﮐ ﺪﻫﺍﻮﺨﻧ ﺭﺎﮐ ﻪﺘﺷﺭ ﮏﯾ ﻂﺳﻭ ﺭﺩ ﺕﺭﺎﺒﻋ ﮏﯾ ﻦﺘﺴﮑﺷ ﺎﻣﺍ

2 ﻝﺎﺜﻣ

let x = "Hello
World!";

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Common JavaScript Mistakes</h2>
<p>Breaking a statement in the middle of a string will not work:</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = "Hello 
World!";
</script>

</body>
</html>

:ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ "ﺶﻠﺳﺍ ﮏﺑ" ﺯﺍ ﺪﯾﺎﺑ ،ﺪﯿﻨﮑﺸﺑ ﻪﺘﺷﺭ ﮏﯾ ﺭﺩ ﺍﺭ ﺕﺭﺎﺒﻋ ﮏﯾ ﺪﯾﺎﺑ ﺮﮔﺍ

3 ﻝﺎﺜﻣ

let x = "Hello \
World!";

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Common JavaScript Mistakes</h2>

<p>You must use a "backslash" if you must break a statement in a string:</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = "Hello \
World!";
</script>

</body>
</html>

ﻩﺎﺒﺘﺷﺍ ﻝﻮﮔﺮﯾﻭ ﻪﻄﻘﻧ ﻥﺩﺍﺩ ﺭﺍﺮﻗ

ﺪﺷ ﺪﻫﺍﻮﺧ ﺍﺮﺟﺍ ﻪﺑ ﻪﺟﻮﺗ ﻥﻭﺪﺑ ﺪﮐ ﮎﻮﻠﺑ ﻦﯾﺍ ،ﺎﺠﺑﺎﻧ ﻝﻮﮔﺮﯾﻭ ﻪﻄﻘﻧ ﮏﯾ ﻞﯿﻟﺩ ﻪﺑ

if (x == 19);
{
    // code block  
}

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Common JavaScript Mistakes</h2>

<p id="demo"></p>

<script>
let x = 5;
if (x == 19);
{
document.getElementById("demo").innerHTML = "Hello";
}
</script>

</body>
</html>

ﺖﺸﮔﺯﺎﺑ ﻪﯿﻧﺎﯿﺑ ﻦﺘﺴﮑﺷ

ﺪﻨﮐﯽﻣ ﻪﺘﺴﺑ ﺖﻤﺴﻗ ﺭﺩ ﺭﺎﮐﺩﻮﺧ ﺭﻮﻃﻪﺑ ﺍﺭ ﺭﻮﺘﺳﺩ ﮏﯾ ﻪﮐ ﺖﺳﺍ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺽﺮﻓﺶﯿﭘ

:ﺩﺍﺩ ﺪﻨﻫﺍﻮﺧ ﻥﺎﺸﻧ ﺍﺭ ﯽﻧﺎﺴﮑﯾ ﻪﺠﯿﺘﻧ ﻝﺎﺜﻣ ﻭﺩ ﻦﯾﺍ ،ﻞﯿﻟﺩ ﻦﯿﻤﻫ ﻪﺑ

1 ﻝﺎﺜﻣ

function myFunction(a) {
    let power = 10  
  return a * power
}

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Common JavaScript Mistakes</h2>

<p>This example will return a correct result:</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = myFunction(55);
function myFunction(a) {
  let power = 10
  return a * power
}
</script>

</body>
</html>

2 ﻝﺎﺜﻣ

function myFunction(a) {
    let power = 10;
  return a * power;
}

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Common JavaScript Mistakes</h2>

<p>This example will return a correct result:</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = myFunction(55);
function myFunction(a) {
  let power = 10;
  return a * power;
}
</script>

</body>
</html>

.ﺪﯿﻨﮐ ﻢﯿﺴﻘﺗ ﻂﺧ ﻭﺩ ﻪﺑ ﺍﺭ ﺕﺭﺎﺒﻋ ﮏﯾ ﻪﮐ ﺪﻫﺩ ﯽﻣ ﺍﺭ ﻥﺎﮑﻣﺍ ﻦﯾﺍ ﺎﻤﺷ ﻪﺑ ﻦﯿﻨﭽﻤﻫ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ

:ﺪﻫﺩ ﯽﻣ ﻥﺎﺸﻧ ﺍﺭ ﻪﺠﯿﺘﻧ ﻥﺎﻤﻫ ﺰﯿﻧ 3 ﻝﺎﺜﻣ ،ﻞﯿﻟﺩ ﻦﯿﻤﻫ ﻪﺑ

3 ﻝﺎﺜﻣ

function myFunction(a) {
    let
  power = 10;  
  return a * power;
}

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Common JavaScript Mistakes</h2>

<p>This example will return a correct result:</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = myFunction(55);
function myFunction(a) {
  let
  power = 10;
  return a * power;
}
</script>

</body>
</html>

ﺪﯿﻨﮑﺸﺑ ﺪﻨﻧﺎﻣ ﻂﺧ ﻭﺩ ﺭﺩ ﺍﺭ return ﺕﺭﺎﺒﻋ ﺮﮔﺍ ﺪﺘﻓﺍﯽﻣ ﯽﻗﺎﻔﺗﺍ ﻪﭼ ،ﺎﻣﺍ

4 ﻝﺎﺜﻣ

function myFunction(a) {
    let
  power = 10;  
  return
  a * power;
}

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Common JavaScript Mistakes</h2>

<p>This example will return undefined:</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = myFunction(55);
function myFunction(a) {
  let
  power = 10;
  return
  a * power;
}
</script>

</body>
</html>

!ﺪﻧﺍﺩﺮﮔﯽﻣﺮﺑ ﺍﺭ ﻩﺪﺸﻧ ﻒﯾﺮﻌﺗ<"code class="w3-codespan> ﻊﺑﺎﺗ

:ﺖﺳﺍ ﻦﯾﺍ ﺎﻤﺷ ﺭﻮﻈﻨﻣ ﺩﺮﮐ ﯽﻣ ﺮﮑﻓ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺍﺮﯾﺯ ؟ﺍﺮﭼ

5 ﻝﺎﺜﻣ

function myFunction(a) {
    let
  power = 10;  
  return;
  a * power;
}

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>Common JavaScript Mistakes</h2>

<p>This example will return undefined:</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = myFunction(55);
function myFunction(a) {
  let
  power = 10;
  return;
  a * power;
}
</script>

</body>
</html>

ﺢﯿﺿﻮﺗ

:ﺪﻨﻧﺎﻣ ﺪﺷﺎﺑ ﺺﻗﺎﻧ ﺕﺭﺎﺒﻋ ﮏﯾ ﺮﮔﺍ

let

:ﺪﻨﮐ ﻞﻣﺎﮐ ﺍﺭ ﺕﺭﺎﺒﻋ ﯼﺪﻌﺑ ﻂﺧ ﻥﺪﻧﺍﻮﺧ ﺎﺑ ﺪﻨﮐ ﯽﻣ ﯽﻌﺳ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ

 power = 10;

:ﺖﺳﺍ ﻞﻣﺎﮐ ﻪﯿﻧﺎﯿﺑ ﻦﯾﺍ ﻪﮐ ﯽﯾﺎﺠﻧﺁ ﺯﺍ ﺎﻣﺍ

 return

:ﺩﺪﻨﺑ ﯽﻣ ﺮﯾﺯ ﺕﺭﻮﺻ ﻪﺑ ﺍﺭ ﻥﺁ ﺭﺎﮐﺩﻮﺧ ﺕﺭﻮﺻ ﻪﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ

 return;

ﺖﺳﺍ ﯼﺭﺎﯿﺘﺧﺍ ﻝﻮﮔﺮﯾﻭ ﻪﻄﻘﻧ ﺎﺑ (ﻥﺩﺍﺩ ﻥﺎﯾﺎﭘ) ﺕﺍﺭﺎﺒﻋ ﻦﺘﺴﺑ ﺍﺮﯾﺯ ﺪﺘﻓﺍﯽﻣ ﻕﺎﻔﺗﺍ ﻦﯾﺍ

ﺍﺮﯾﺯ ،ﺩﺪﻨﺑ ﯽﻣ ﻂﺧ ﯼﺎﻬﺘﻧﺍ ﺭﺩ ﺍﺭ return ﺕﺭﺎﺒﻋ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ

.ﺪﯿﻨﮑﺸﻧ ﺍﺭ ﺖﺸﮔﺯﺎﺑ ﻪﯿﻧﺎﯿﺑ ﺰﮔﺮﻫ


ﻩﺪﺷ ﯼﺭﺍﺬﮕﻣﺎﻧ ﯼﺎﻫ ﻪﯾﺎﻤﻧ ﺎﺑ ﺎﻫ ﻪﯾﺍﺭﺁ ﻪﺑ ﯽﺳﺮﺘﺳﺩ

.ﺪﻨﻨﮐ ﯽﻣ ﯽﻧﺎﺒﯿﺘﺸﭘ ﻩﺪﺷ ﯼﺭﺍﺬﮕﻣﺎﻧ ﯼﺎﻫ ﻪﯾﺎﻤﻧ ﺎﺑ ﯽﯾﺎﻫ ﻪﯾﺍﺭﺁ ﺯﺍ ﯽﺴﯾﻮﻧ ﻪﻣﺎﻧﺮﺑ ﯼﺎﻫ ﻥﺎﺑﺯ ﺯﺍ ﯼﺭ

ﺪﻧﻮﺷ ﯽﻣ ﻩﺪﯿﻣﺎﻧ ﯽﻨﻤﺠﻧﺍ ﺪﻧﺭﺍﺩ ﻩﺪﺷ ﯼﺭﺍﺬﮕﻣﺎﻧ ﯼﺎﻫ ﺺﺧﺎﺷ ﻪﮐ ﯽﯾﺎﻫ ﻪﯾﺍﺭﺁ

.ﺪﻨﮐ ﯽﻤﻧ ﯽﻧﺎﺒﯿﺘﺸﭘ ﻩﺪﺷ ﯼﺭﺍﺬﮕﻣﺎﻧ ﯼﺎﻫ ﻪﯾﺎﻤﻧ ﯼﺍﺭﺍﺩ ﯼﺎﻫ ﻪﯾﺍﺭﺁ ﺯﺍ ﺖﭙﯾﺮﮑﺳ

:ﺪﻨﻨﮐ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﻩﺪﺷ ﯼﺭﺍﺬﮔ ﻩﺭﺎﻤﺷ ﯼﺎﻫ ﺺﺧﺎﺷ ﺯﺍ ﺎﻫ ﻪﯾﺍﺭﺁ

ﻝﺎﺜﻣ

const person = [];
person[0] = "John";
person[1] = "Doe";
person[2] = 46;
person.length;       
 // person.length will return 3
person[0];           
 // person[0] will return "John"

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>

<p id="demo"></p>

<script>
const person = [];
person[0] = "John";
person[1] = "Doe";
person[2] = 46; 
document.getElementById("demo").innerHTML =
person[0] + " " + person.length;
</script>

</body>
</html>

.ﺪﻨﻨﮐﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﻩﺪﺷ ﯼﺭﺍﺬﮔﻡﺎﻧ ﯼﺎﻫﺺﺧﺎﺷ ﺯﺍ ءﺎﯿﺷ

ﺪﻨﮐ ﯽﻣ ﻒﯾﺮﻌﺗ ﻩﺭﺎﺑﻭﺩ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ،ﻪﯾﺍﺭﺁ ﮏﯾ ﻪﺑ ﯽﺳﺮﺘﺳﺩ ﻡﺎﮕﻨﻫ ،ﺪﯿﻨﮐ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﻡﺎﻧ ﺎﺑ ﻪﯾ

ﺪﻨﻨﮐﯽﻣ ﺪﯿﻟﻮﺗ ﻩﺪﺸﻧ ﻒﯾﺮﻌﺗ ﺎﯾ ﻪﯾﺍﺭﺁ ﯼﺎﻫﯽﮔﮋﯾﻭ ﻭ ﺎﻫﺵﻭﺭ ،ﺭﺎﮐﺩﻮﺧ ﺩﺪﺠﻣ ﻒﯾﺮﻌﺗ ﺯ

: ﻝﺎﺜﻣ

const person = [];
person["firstName"] = "John";
person["lastName"] = "Doe";
person["age"] = 46;
person.length;      // person.length will 
 return 0
person[0];          
 // person[0] will return undefined

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>

<p>If you use a named index when accessing an array, JavaScript will redefine the array to a standard object, and some array methods and properties will produce undefined or incorrect results.</p>

<p id="demo"></p>

<script>
const person = [];
person["firstName"] = "John";
person["lastName"] = "Doe";
person["age"] = 46; 
document.getElementById("demo").innerHTML =
person[0] + " " + person.length;
</script>

</body>
</html>


ﺎﻣﺎﮐ ﺎﺑ ﻒﯾﺭﺎﻌﺗ ﻪﺑ ﻥﺩﺍﺩ ﻥﺎﯾﺎﭘ

.ﺪﻨﺘﺴﻫ ﯽﻧﻮﻧﺎﻗ ECMAScript 5 ﺭﺩ ﻪﯾﺍﺭﺁ ﻭ ﯽﺷ ﻒﯾﺮﻌﺗ ﺭﺩ ﯽﯾﺎﻬﺘﻧﺍ ﯼﺎﻫﺎﻣﺎﮐ

:ﯽﺷ ﻝﺎﺜﻣ

person = {firstName:"John", lastName:"Doe", age:46,}

:ﻪﯾﺍﺭﺁ ﻝﺎﺜﻣ

points = [40, 100, 1, 5, 25, 10,];

!!ﺭﺍﺪﺸﻫ

.ﺩﻮﺷ ﯽﻣ ﺏﺍﺮﺧ 8 ﺭﺭﻮﻠﭙﺴﮐﺍ ﺖﻧﺮﺘﻨﯾﺍ

JSON ﺪﻫﺩ ﯽﻤﻧ ﺍﺭ ﯽﯾﺎﻬﺘﻧﺍ ﯼﺎﻫﺎﻣﺎﮐ ﻩﺯﺎﺟﺍ.

JSON:

person = {"firstName":"John", "lastName":"Doe", "age":46}

JSON:

points = [40, 100, 1, 5, 25, 10];

Undefined Null ﺖﺴﯿﻧ

.ﺪﻨﺷﺎﺑ ﻩﺪﺸﻧ ﻒﯾﺮﻌﺗ<"code class="w3-codespan> ﺪﻨﻧﺍﻮﺗﯽﻣ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯼﺎﻫﺪﺘﻣ

.ﺪﻨﺷﺎﺑ ﻪﺘﺷﺍﺩ null ﺭﺍﺪﻘﻣ ﺪﻨﻧﺍﻮﺗ ﯽﻣ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﻟﺎﺧ

.ﺪﻨﮐ ﺭﺍﻮﺷﺩ ﯽﻤﮐ ﺍﺭ ﯽﺷ ﮏﯾ ﻥﺩﻮﺑ ﯽﻟﺎﺧ ﺶﯾﺎﻣﺯﺁ ﺪﻧﺍﻮﺗ ﯽﻣ ﻦﯾﺍ

:ﺪﯿﻨﮐ ﺶﯾﺎﻣﺯﺁ ﺍﺭ ﯽﺷ ﮏﯾ ﺩﻮﺟﻭ ،ﺖﺳﺍ ﻩﺪﺸﻧ ﻒﯾﺮﻌﺗ <"code class="w3-codespan> ﻥﺁ ﻉﻮﻧ ﺎ

: ﻝﺎﺜﻣ

if (typeof myObj === "undefined") 

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Objects</h2>

<p>To test if an object does not exist, test if the type is undefined:</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = typeof myObj === "undefined";
</script>

</body>
</html>

ﺮﮔﺍ ﺍﺮﯾﺯ ،ﺖﺳﺍ null ﯽﺷ ﮏﯾ ﺎﯾﺁ ﻪﮐ ﺪﯿﻨﮐ ﺶﯾﺎﻣﺯﺁ ﺪﯿﻧﺍﻮﺗu2

:ﻂﻠﻏ

if (myObj === null)  

،ﺖﺴﯿﻧ null ﯽﺷ ﮏﯾ ﺎﯾﺁ ﻪﮐ ﺪﯿﻨﮐ ﺶﯾﺎﻣﺯﺁ ﺪﯾﺎﺑ ،ﻞﮑﺸﻣ ﻦﯾﺍ ﻞ

:ﺪﻨﮐ ﺩﺎﺠﯾﺍ ﺎﻄﺧ ﮏﯾ ﺪﻧﺍﻮﺗ ﯽﻣ ﻢﻫ ﺯﻮﻨﻫ ﻦﯾﺍ ﺎﻣﺍ

:ﻂﻠﻏ

if (myObj !== null && typeof myObj 
  !== "undefined")  

ﺪﯿﻨﮐ ﺶﯾﺎﻣﺯﺁ ﺍﺭ undefined ﺪﯿﻧﺍﻮﺘﺑ ﻪﮑﻨﯾﺍ ﺯﺍ ﻞﺒﻗ ﺪﯾﺎﺑ ،

: ﺖﺳﺭﺩ

if (typeof myObj !== "undefined" && myObj !== null) 

→ ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Objects</h2>

<p>If you want to test if an object is not null, you must test if it not undefined first.</p>

<p id="demo"></p>

<script>
document.getElementById("demo").innerHTML = typeof myObj !== "undefined" && myObj !== null;
</script>

</body>
</html>

تمامی حقوق محفوظ است. © BasicIT.org • 2023-2024