ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﭻﯿﺋﻮﺳ ﻪﯿﻧﺎﯿﺑ


فهرست مطالب

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


.ﺩﻮﺷ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﻒﻠﺘﺨﻣ ﻂﯾﺍﺮﺷ ﺱﺎﺳﺍ ﺮﺑ ﻒﻠﺘﺨﻣ ﺕﺎﻣﺍﺪﻗﺍ ﻡﺎﺠﻧﺍ ﯼﺍﺮﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﭻﯿﺋﻮﺳ ﻪﯿﻧﺎﯿﺑ

.ﺪﯿﻨﮐ ﻩﺩﺎﻔﺘﺳﺍ ﺍﺮﺟﺍ ﯼﺍﺮﺑ ﺪﮐ ﯼﺎﻫ ﮎﻮﻠﺑ ﺯﺍ ﯼﺭﺎﯿﺴﺑ ﺯﺍ ﯽﮑﯾ ﺏﺎﺨﺘﻧﺍ ﯼﺍﺮﺑ ﻮﺤﻧ

switch(expression) {
  case x:
     // code block
        break;
  case y:
     // code block
     break;
  default:
      // code block
 }

:ﺪﻨﮑﯿﻣ ﺭﺎﮐ ﯼﺭﻮﺠﻨﯾﺍ

  • .ﺩﻮﺷ ﯽﻣ ﯽﺑﺎﯾﺯﺭﺍ ﺭﺎﺑ ﮏﯾ ﭻﯿﺋﻮﺳ ﺕﺭﺎﺒﻋ

  • .ﺩﻮﺷ ﯽﻣ ﻪﺴﯾﺎﻘﻣ ﺩﺭﻮﻣ ﺮﻫ ﺮﯾﺩﺎﻘﻣ ﺎﺑ ﺕﺭﺎﺒﻋ ﺭﺍﺪﻘﻣ

  • .ﺩﻮﺷ ﯽﻣ ﺍﺮﺟﺍ ﻂﺒﺗﺮﻣ ﺪﮐ ﮎﻮﻠﺑ ،ﺪﺷﺎﺑ ﻪﺘﺷﺍﺩ ﺩﻮﺟﻭ ﺖﻘﺑﺎﻄﻣ ﺮﮔﺍ

  • .ﺩﻮﺷ ﯽﻣ ﺍﺮﺟﺍ ﺽﺮﻓ ﺶﯿﭘ ﺪﮐ ﮎﻮﻠﺑ ،ﺪﺷﺎﺑ ﻪﺘﺷﺍﺪﻧ ﺩﻮﺟﻭ ﺖﻘﺑﺎﻄﻣ ﺮﮔﺍ

ﻝﺎﺜﻣ

ﻦﯿﺑ ﯼﺩﺪﻋ ﻥﺍﻮﻨﻋ ﻪﺑ ﺍﺭ ﻪﺘﻔﻫ ﺯﻭﺭ getDay() ﺵﻭﺭ

(.. 2=ﻪﺒﻨﺷ ﻪﺳ ،1=ﻪﺒﻨﺷﻭﺩ ،0=ﻪﺒﻨﺸﮑﯾ)

:ﺪﻨﮐ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﻪﺘﻔﻫ ﺯﻭﺭ ﻡﺎﻧ ﻪﺒﺳﺎﺤﻣ ﯼﺍﺮﺑ ﻪﺘﻔﻫ ﺯﻭﺭ ﻩﺭﺎﻤﺷ ﺯﺍ ﻝﺎﺜﻣ ﻦﯾﺍ

switch (new Date().getDay()) {
  case 0:
    day = "Sunday";
    break;
  case 1:
      day = "Monday";
     break;
  case 2:
       day = "Tuesday";
      break;
  case 3:
    day = "Wednesday";
      break;
  case 4:
      day = "Thursday";
     break;
  case 5:
      day = "Friday";
      break;
  case 6:
    day = "Saturday";
}

:ﺩﻮﺑ ﺪﻫﺍﻮﺧ ﺯﻭﺭ ﻪﺠﯿﺘﻧ

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

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript switch</h2>

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

<script>
let day;
switch (new Date().getDay()) {
  case 0:
    day = "Sunday";
    break;
  case 1:
    day = "Monday";
    break;
  case 2:
    day = "Tuesday";
    break;
  case 3:
    day = "Wednesday";
    break;
  case 4:
    day = "Thursday";
    break;
  case 5:
    day = "Friday";
    break;
  case  6:
    day = "Saturday";
}
document.getElementById("demo").innerHTML = "Today is " + day;
</script>

</body>
</html>


ﻦﺘﺴﮑﺷ ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ

ﺪﯿﺳﺭ ﺖﺴﮑﺷ<"code class="w3-codespan> ﮏﯾ ﻪﺑ ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﯽﺘﻗﻭ

.ﺪﻨﮐ ﯽﻣ ﻒﻗﻮﺘﻣ ﺍﺭ ﭻﯿﺋﻮﺳ ﮎﻮﻠﺑ ﻞﺧﺍﺩ ﯼﺍﺮﺟﺍ ﺭﺎﮐ ﻦﯾﺍ

.(ﺪﺳﺭ ﯽﻣ ﻥﺎﯾﺎﭘ ﻪﺑ) ﺩﻮﺷ ﯽﻣ ﻪﺘﺴﮑﺷ ﺎﺠﻧﺁ ﺭﺩ ﮎﻮﻠﺑ ﻝﺎﺣ ﺮﻫ ﻪﺑ .ﺖﺴﯿﻧ ﭻﯿﺋﻮﺳ ﮎﻮﻠﺑ ﺭﺩ ﺩﺭﻮﻣ ﻦﯾﺮﺧﺁ

ﻪﺟﻮﺗ: ﺭﻮﺘﺳﺩ ﺮﮔﺍ break ﺮﮔﺍ ﯽﺘﺣ ﯼﺪﻌﺑ ﺩﺭﻮﻣ ،ﺪﯿﻨﮐ ﻑﺬﺣ ﺍﺭ


ﺽﺮﻓ ﺶﯿﭘ ﯼﺪﯿﻠﮐ ﻪﻤﻠﮐ

.ﺩﻮﺷ ﺍﺮﺟﺍ ﺪﮐ ﺩﻮﺟﻭ ﻡﺪﻋ ﺕﺭﻮﺻ ﺭﺩ ﻪﮐ ﺪﻨﮐﯽﻣ ﺺﺨﺸﻣ ﺍﺭ ﯼﺪﮐ ﺽﺮﻓﺶﯿﭘ<"code clas

ﻝﺎﺜﻣ

ﻦﯿﺑ ﯼﺩﺪﻋ ﻥﺍﻮﻨﻋ ﻪﺑ ﺍﺭ ﻪﺘﻔﻫ ﺯﻭﺭ getDay() ﺵﻭﺭ

:ﺪﯿﺴﯾﻮﻨﺑ ﺽﺮﻓ ﺶﯿﭘ ﻡﺎﯿﭘ ﮏﯾ ،ﺖﺳﺍ (0) ﻪﺒﻨﺸﮑﯾ ﻪﻧ ﻭ (6) ﻪﺒﻨﺷ ﻪﻧ ﺯﻭﺮﻣﺍ ﺮﮔﺍ

switch (new Date().getDay()) {
   
case 6:
      text = "Today is Saturday";
    break; 
  case 0:
      text = "Today is Sunday";
      break; 
   
default: 
      text = "Looking forward to the Weekend";
}

:ﺩﻮﺑ ﺪﻫﺍﻮﺧ ﻦﺘﻣ ﻪﺠﯿﺘﻧ

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

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript switch</h2>

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

<script>
let text;
switch (new Date().getDay()) {
  case 6:
    text = "Today is Saturday";
    break;
  case 0:
    text = "Today is Sunday";
    break;
  default:
    text = "Looking forward to the Weekend";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

.ﺪﺷﺎﺑ ﭻﯿﺋﻮﺳ ﮏﯾ ﺭﺩ ﺩﺭﻮﻣ ﻦﯾﺮﺧﺁ ﺖﺴﯿﻧ ﻡﺯﻻ ﺽﺮﻓﺶﯿﭘ<"code class="w3-codespan> ﺩﺭ

ﻝﺎﺜﻣ

switch (new Date().getDay()) {
  default: 
        text = "Looking forward to the Weekend";
    
  break;
  case 6:
        text = "Today is Saturday";
        break; 
  case 0:
        text = "Today is Sunday";
  }

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

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript switch</h2>

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

<script>
let text;
switch (new Date().getDay()) {
  default:
    text = "Looking forward to the Weekend";
    break;
  case 6:
    text = "Today is Saturday";
    break;
  case 0:
    text = "Today is Sunday";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

.ﺪﯿﻫﺩ ﻥﺎﯾﺎﭘ ﺖﺣﺍﺮﺘﺳﺍ ﮏﯾ ﺎﺑ ﺍﺭ ﺽﺮﻓﺶﯿﭘ ﻑﻭﺮﺣ ﻪﮐ ﺪﯿﺷﺎﺑ ﻪﺘﺷﺍﺩ ﺩﺎﯾ ﻪﺑ ،ﺖﺴﯿﻧ ﭻﯿﺋﻮﺳ ﮎﻮﻠﺑ


ﺞﯾﺍﺭ ﺪﮐ ﯼﺎﻫ ﮎﻮﻠﺑ

ﺪﯿﻫﺍﻮﺧ ﯽﻣ ﺕﻭﺎﻔﺘﻣ ﺕﺎﻗﻭﺍ ﯽﻫﺎﮔ

ﺪﻧﺭﺍﺬﮔ ﯽﻣ ﮎﺍﺮﺘﺷﺍ ﻪﺑ 6 ﻭ 0 ﻭ ﺪﻧﺭﺍﺬﮔ ﯽﻣ ﮎﺍﺮﺘﺷﺍ ﻪﺑ ﺍﺭ ﯽﻧﺎﺴﮑﯾ ﺪﮐ ﮎﻮﻠﺑ 5 ﻭ 4 ،ﻝﺎﺜﻣ ﻦﯾﺍ ﺭﺩ

ﻝﺎﺜﻣ

switch (new Date().getDay()) {
  case 4:
  case 5:
    text = "Soon it is Weekend";
      break; 
  case 0:
  case 6:
      text = "It is Weekend";
    break;
  default: 
    text = "Looking forward to the Weekend";
  }

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

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript switch</h2>

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

<script>
let text;
switch (new Date().getDay()) {
  case 4:
  case 5:
    text = "Soon it is Weekend";
    break;
  case 0:
  case 6:
    text = "It is Weekend";
    break;
  default:
    text = "Looking forward to the Weekend";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

ﺕﺎﯿﺋﺰﺟ ﺮﯿﯿﻐﺗ

.ﺩﻮﺷﯽﻣ ﺏﺎﺨﺘﻧﺍ ﻦﯿﻟﻭﺍ ﺩﺭﻮﻣ ،ﺪﺷﺎﺑ ﻪﺘﺷﺍﺩ ﺖﻘﺑﺎﻄﻣ ﺩﺭﻮﻣ ﺭﺍﺪﻘﻣ ﺎﺑ ﺩﺭﻮﻣ

.ﺪﻫﺩﯽﻣ ﻪﻣﺍﺩﺍ ﺽﺮﻓﺶﯿﭘ ﺐﺴﭼﺮﺑ ﻪﺑ ﻪﻣﺎﻧﺮﺑ ،ﺪﺸﻧ ﺖﻓﺎﯾ ﯽﻘﺒﻄﻨﻣ ﺩﺭﻮﻣ

.ﺪﻫﺩﯽﻣ ﻪﻣﺍﺩﺍ ﭻﯿﺋﻮﺳ ﺯﺍ ﺲﭘ ﺕﺭﺎﺒﻋ ﻪﺑ ﻪﻣﺎﻧﺮﺑ ،ﺪﺸﻧ ﺖﻓﺎﯾ ﯽﺿﺮﻓﺶﯿﭘ


ﻖﯿﻗﺩ ﻪﺴﯾﺎﻘﻣ

.ﺪﻨﻨﮐ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ (===) ﺖﺨﺳ ﻪﺴﯾﺎﻘﻣ ﺯﺍ ﭻﯿﺋﻮﺳ ﺩﺭﺍﻮﻣ

.ﺪﻨﺷﺎﺑ ﻪﺘﺷﺍﺩ ﺖﻘﺑﺎﻄﻣ ﺎﺗ ﺪﻨﺷﺎﺑ ﻉﻮﻧ ﮏﯾ ﺯﺍ ﺪﯾﺎﺑ ﺮﯾﺩﺎﻘﻣ

.ﺪﻨﺷﺎﺑ ﻉﻮﻧ ﮏﯾ ﺯﺍ ﺎﻫﺪﻧﻮﻠﻤﻋ ﻪﮐ ﺪﺷﺎﺑ ﺖﺳﺭﺩ ﺪﻧﺍﻮﺗ ﯽﻣ ﯽﻧﺎﻣﺯ ﺎﻬﻨﺗ ﻖﯿﻗﺩ ﻪﺴﯾﺎﻘﻣ ﮏﯾ

:ﺖﺷﺍﺩ ﺪﻫﺍﻮﺨﻧ ﺩﻮﺟﻭ x ﯼﺍﺮﺑ ﯽﻘﯿﺒﻄﺗ ﭻﯿﻫ ﻝﺎﺜﻣ ﻦﯾﺍ ﺭﺩ

ﻝﺎﺜﻣ

 let x = "0";
switch (x) {
  case 0:
    text = "Off";
      break;
  case 1:
    text = "On";
    break;
    default:
    
  text = "No value found";
} 

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

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript switch</h2>

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

<script>
let x = "0";

switch (x) {
  case 0:
    text = "Off";
    break;
  case 1:
    text = "On";
    break;
  default:
    text = "No value found";
}
document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>


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