JavaScript if...else
ဥပမာ
နာရီ 20 ထက်နည်းပါက "ကောင်းသောနေ့" ကိုထုတ်ပေးပါ-
let hour = new Date().getHours();
if (hour < 20) {
document.getElementById("demo").innerHTML = "Good day";
}
"ကောင်းသောနေ့" သို့မဟုတ် "ကောင်းသောညနေခင်း" ကိုထုတ်ပေးသည်-
let hour = new Date().getHours();
if (hour < 20) {
greeting = "Good day";
}
else {
greeting = "Good evening";
}
အောက်တွင် နောက်ထပ် ဥပမာများ။
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
သတ်မှတ်ထားသောအခြေအနေမှန်ပါက if/else ကြေငြာချက်သည် ကုဒ်ပိတ်ဆို့ခြင်းကို လုပ်ဆောင်သည်။ အခြေအနေသည် မှားယွင်းပါက၊ အခြားကုဒ်ဘလော့ဂ်ကို လုပ်ဆောင်နိုင်သည်။
if/else ထုတ်ပြန်ချက်သည် မတူညီသောအခြေအနေများကိုအခြေခံ၍ မတူညီသောလုပ်ဆောင်ချက်များကိုလုပ်ဆောင်ရန်အသုံးပြုသည့် JavaScript ၏ "Conditional" ဖော်ပြချက်များ၏တစ်စိတ်တစ်ပိုင်းဖြစ်သည်။
JavaScript တွင် ကျွန်ုပ်တို့တွင် အောက်ပါအခြေအနေဆိုင်ရာ ထုတ်ပြန်ချက်များရှိသည်။
- သတ်မှတ်ထားသော အခြေအနေမှန်ပါက လုပ်ဆောင်ရမည့် ကုဒ်တုံးတစ်ခုကို သတ်မှတ်ရန် if ကို အသုံးပြု ပါ။
- တူညီသောအခြေအနေသည် မှားယွင်းပါက လုပ်ဆောင်ရန် ကုဒ်ပိတ်ဆို့ခြင်းကို သတ်မှတ်ရန် အခြား ကို အသုံးပြု ပါ။
- ပထမအခြေအနေသည် မှားယွင်းနေပါက စမ်းသပ်ရန် အခြေအနေအသစ်ကို သတ်မှတ်ရန် else if ကို သုံး ပါ။
- လုပ်ဆောင်ရမည့် ကုဒ်အများအပြားထဲမှ တစ်ခုကို ရွေးချယ်ရန် ခလုတ်ကို အသုံးပြု ပါ။
အထားအသို
အခြေအနေတစ်ခုမှန်ပါ က လုပ်ဆောင်ရန် if statement သည် ကုဒ်ပိတ်ဆို့ခြင်းကို သတ်မှတ်သည်-
if (condition) {
// block of code to be executed if the condition is true
}
အခြေအနေသည် မှားယွင်းပါက လုပ်ဆောင်ရန် အခြား ဖော်ပြချက်သည် ကုဒ်ပိတ်ဆို့ခြင်းကို သတ်မှတ်သည်-
if (condition) {
// block of code to be executed if the condition is true
}
else {
// block of code to be executed if the condition is false
}
အကယ်၍ ပထမ အခြေအနေသည် မှားယွင်းပါက၊
if (condition1) {
// block of code to be executed if condition1 is true
}
else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
ကန့်သတ်တန်ဖိုးများ
Parameter | Description |
---|---|
condition | Required. An expression that evaluates to true or false |
နောက်ထပ် ဥပမာများ
အချိန်က 10:00 ထက်နည်းရင် "မင်္ဂလာနံနက်ခင်းပါ" နှုတ်ခွန်းဆက်စကား ဖန်တီးပါ၊ ဒါပေမယ့် အချိန်က 20:00 ထက်နည်းရင် "ကောင်းသောနေ့" လို့ နှုတ်ခွန်းဆက်ပါ၊ မဟုတ်ရင် "မင်္ဂလာညချမ်းပါ"
var time = new Date().getHours();
if (time < 10) {
greeting = "Good morning";
}
else if (time < 20) {
greeting = "Good day";
}
else {
greeting = "Good evening";
}
စာရွက်စာတမ်းရှိ ပထမ <div> ဒြပ်စင်တွင် "myDIV" ၏ အိုင်ဒီပါရှိပါက ၎င်း၏ဖောင့်အရွယ်အစားကို ပြောင်းပါ-
var x = document.getElementsByTagName("DIV")[0];
if (x.id === "myDIV") {
x.style.fontSize = "30px";
}
အသုံးပြုသူသည် ပုံပေါ်တွင်နှိပ်ပါက <img> ဒြပ်စင်တစ်ခု၏ အရင်းအမြစ် attribute (src) ၏တန်ဖိုးကို ပြောင်းလဲပါ-
<img id="myImage" onclick="changeImage()" src="pic_bulboff.gif" width="100" height="180">
<script>
function changeImage() {
var image = document.getElementById("myImage");
if (image.src.match("bulbon")) {
image.src = "pic_bulboff.gif";
} else {
image.src = "pic_bulbon.gif";
}
}
</script>
အသုံးပြုသူထည့်သွင်းမှုအပေါ်အခြေခံ၍ မက်ဆေ့ခ်ျကိုပြသပါ-
var letter = document.getElementById("myInput").value;
var text;
// If the letter is "c"
if (letter === "c") {
text = "Spot on! Good job!";
// If the letter is "b" or "d"
} else if (letter === "b" || letter === "d") {
text = "Close, but not close enough.";
// If the letter is anything else
} else {
text = "Waaay off..";
}
ထည့်သွင်းဒေတာကို အတည်ပြုပါ-
var x, text;
// Get the value of the input field with id="numb"
x = document.getElementById("numb").value;
// If x is Not a Number or less than 1 or greater than 10, output "input is not valid"
// If x is a number between 1 and 10, output "Input OK"
if (isNaN(x) || x < 1 || x > 10) {
text = "Input not valid";
} else {
text = "Input OK";
}
ဆက်စပ်စာမျက်နှာများ
JavaScript သင်ခန်းစာ- JavaScript If...Else Statements
JavaScript ကျူတိုရီရယ်- JavaScript Switch Statement
Browser ပံ့ပိုးမှု
if...else
ECMAScript1 (ES1) အင်္ဂါရပ်တစ်ခုဖြစ်သည်။
ES1 (JavaScript 1997) ကို ဘရောက်ဆာအားလုံးတွင် အပြည့်အဝ ပံ့ပိုးထားသည်။
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |