JavaScript eğer...else
Örnek
Saat 20'den azsa, "İyi günler" çıktısını alın:
let hour = new Date().getHours();
if (hour < 20) {
document.getElementById("demo").innerHTML = "Good day";
}
Çıktı "İyi günler" veya "İyi akşamlar":
let hour = new Date().getHours();
if (hour < 20) {
greeting = "Good day";
}
else {
greeting = "Good evening";
}
Aşağıda daha fazla örnek.
Tanım ve Kullanım
if/else ifadesi, belirtilen bir koşul doğruysa bir kod bloğunu yürütür. Koşul yanlışsa, başka bir kod bloğu yürütülebilir.
if/else ifadesi, farklı koşullara dayalı olarak farklı eylemleri gerçekleştirmek için kullanılan JavaScript'in "Koşullu" İfadelerinin bir parçasıdır.
JavaScript'te aşağıdaki koşullu ifadelere sahibiz:
- Belirtilen bir koşul doğruysa yürütülecek bir kod bloğu belirtmek için if kullanın
- Aynı koşul yanlışsa, yürütülecek kod bloğunu belirtmek için else kullanın .
- İlk koşul yanlışsa, test edilecek yeni bir koşul belirtmek için else if öğesini kullanın .
- Yürütülecek birçok kod bloğundan birini seçmek için anahtarı kullanın
Sözdizimi
if ifadesi , bir koşul doğruysa yürütülecek bir kod bloğunu belirtir:
if (condition) {
// block of code to be executed if the condition is true
}
else ifadesi , koşul yanlışsa yürütülecek bir kod bloğunu belirtir:
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
}
İlk koşul yanlışsa, else if ifadesi yeni bir koşul belirtir:
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
}
Parametre Değerleri
Parameter | Description |
---|---|
condition | Required. An expression that evaluates to true or false |
Daha fazla örnek
Saat 10:00'dan azsa, bir "Günaydın" karşılama mesajı oluşturun, değilse, ancak saat 20:00'den azsa, bir "İyi günler" karşılama mesajı oluşturun, aksi takdirde "İyi akşamlar":
var time = new Date().getHours();
if (time < 10) {
greeting = "Good morning";
}
else if (time < 20) {
greeting = "Good day";
}
else {
greeting = "Good evening";
}
Belgedeki ilk <div> öğesinin kimliği "myDIV" ise, yazı tipi boyutunu değiştirin:
var x = document.getElementsByTagName("DIV")[0];
if (x.id === "myDIV") {
x.style.fontSize = "30px";
}
Kullanıcı resme tıklarsa, bir <img> öğesinin kaynak özniteliğinin (src) değerini değiştirin:
<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>
Kullanıcı girişine göre bir mesaj görüntüleyin:
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..";
}
Giriş verilerini doğrulayın:
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";
}
İlgili Sayfalar
JavaScript Eğitimi: JavaScript If...Else İfadeleri
JavaScript Eğitimi: JavaScript Switch Beyanı
Tarayıcı Desteği
if...else
bir ECMAScript1 (ES1) özelliğidir.
ES1 (JavaScript 1997) tüm tarayıcılarda tam olarak desteklenir:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |