Gunakan pernyataan Show Sintaksisjika (kondisi) { Perhatikan bahwa ContohUcapkan sapaan "Selamat siang" jika waktunya kurang dari 18. 00 if (jam < 18) { Hasil salam akan Cobalah sendiri "Pernyataan lainGunakan pernyataan jika (kondisi) { ContohJika waktunya kurang dari 18, buat sapaan "Selamat siang", jika tidak, "Selamat malam" if (jam < 18) { Hasil salam akan Cobalah sendiri "Lain jika PernyataanGunakan pernyataan Sintaksisjika (kondisi1) { ContohJika waktu kurang dari 10. 00, buat salam "Selamat pagi", jika tidak, tetapi waktunya kurang dari 20. 00, buat sapaan "Selamat siang", jika tidak, buat "Selamat malam" Pernyataan if-else atau kondisional akan melakukan beberapa tindakan untuk kondisi tertentu. Jika kondisi memenuhi maka blok tindakan tertentu akan dieksekusi jika tidak maka akan mengeksekusi blok tindakan lain yang memenuhi kondisi tertentu. Pernyataan kontrol seperti itu digunakan untuk menyebabkan aliran eksekusi maju dan bercabang berdasarkan perubahan status program Contoh. Contoh ini menjelaskan pernyataan if dalam Javascript Javascript
if(condition) { // Statements to execute if // condition is true }_0 if(condition) { // Statements to execute if // condition is true }1 if(condition) { // Statements to execute if // condition is true }2 if(condition) { // Statements to execute if // condition is true }3 if(condition) { // Statements to execute if // condition is true }2 if(condition) { // Statements to execute if // condition is true }5 if(condition) { // Statements to execute if // condition is true }6 if(condition) { // Statements to execute if // condition is true }_2 if(condition) { // Statements to execute if // condition is true }2 if(condition) { // Statements to execute if // condition is true }9 if(condition) statement1; statement2; // Here if the condition is true, if block // will consider only statement1 to be inside // its block.0 if(condition) statement1; statement2; // Here if the condition is true, if block // will consider only statement1 to be inside // its block.1 if(condition) statement1; statement2; // Here if the condition is true, if block // will consider only statement1 to be inside // its block.2 if(condition) { // Statements to execute if // condition is true }_2 if(condition) { // Statements to execute if // condition is true }2 if(condition) statement1; statement2; // Here if the condition is true, if block // will consider only statement1 to be inside // its block.5 if(condition) { // Statements to execute if // condition is true }2 if(condition) statement1; statement2; // Here if the condition is true, if block // will consider only statement1 to be inside // its block.7 if(condition) { // Statements to execute if // condition is true }2 if(condition) statement1; statement2; // Here if the condition is true, if block // will consider only statement1 to be inside // its block.9 if (condition) { // Executes this block if // condition is true } else { // Executes this block if // condition is false }0 if(condition) statement1; statement2; // Here if the condition is true, if block // will consider only statement1 to be inside // its block.2 if (condition) { // Executes this block if // condition is true } else { // Executes this block if // condition is false }2 Keluaran I am Not in if Pernyataan bersyarat JavaScript
Kami akan memahami setiap pernyataan bersyarat, sintaksnya, diagram alur, dan contohnya. Silakan merujuk ke Switch Case di artikel JavaScript untuk memahami switch case. Mari kita mulai dengan pernyataan if JavaScript jika-pernyataan. Ini adalah pernyataan bersyarat yang digunakan untuk memutuskan apakah pernyataan atau blok pernyataan tertentu akan dieksekusi atau tidak i. e jika kondisi tertentu benar maka blok pernyataan dijalankan jika tidak Sintaksis if(condition) { // Statements to execute if // condition is true } Pernyataan if menerima nilai boolean – jika nilainya benar maka akan mengeksekusi blok pernyataan di bawahnya. Jika kita tidak menyediakan kurung kurawal '{' dan '}' setelah if( condition ) maka secara default if pernyataan menganggap satu pernyataan langsung berada di dalam bloknya. Sebagai contoh, if(condition) statement1; statement2; // Here if the condition is true, if block // will consider only statement1 to be inside // its block. Flow chart pernyataan kondisi-jika Contoh. Berikut adalah contoh sederhana yang menunjukkan pernyataan if Javascriptif (condition) { // Executes this block if // condition is true } else { // Executes this block if // condition is false }3 if(condition) { // Statements to execute if // condition is true }0 if(condition) { // Statements to execute if // condition is true }1 if(condition) { // Statements to execute if // condition is true }2 if(condition) { // Statements to execute if // condition is true }3 if(condition) { // Statements to execute if // condition is true }2 if(condition) { // Statements to execute if // condition is true }5 i is less than 150
if(condition) { // Statements to execute if // condition is true }2 if(condition) { // Statements to execute if // condition is true }9 i is less than 153 i is less than 154 if(condition) statement1; statement2; // Here if the condition is true, if block // will consider only statement1 to be inside // its block.9 i is less than 156 if(condition) statement1; statement2; // Here if the condition is true, if block // will consider only statement1 to be inside // its block.2 if (condition) { // Executes this block if // condition is true } else { // Executes this block if // condition is false }2
Pernyataan JavaScript if-else. Pernyataan if sendiri memberi tahu kita bahwa jika suatu kondisi benar, ia akan mengeksekusi blok pernyataan dan jika kondisinya salah, itu tidak akan dilakukan. Tetapi bagaimana jika kita ingin melakukan sesuatu yang lain jika kondisinya salah? . Kita dapat menggunakan pernyataan else dengan pernyataan if untuk mengeksekusi blok kode ketika kondisi salah Sintaksis if (condition) { // Executes this block if // condition is true } else { // Executes this block if // condition is false } Flow chart pernyataan if-else Contoh. Contoh ini menjelaskan pernyataan if-else dalam Javascript JavaScriptif (condition) { // Executes this block if // condition is true } else { // Executes this block if // condition is false }3 if(condition) { // Statements to execute if // condition is true }0 if(condition) { // Statements to execute if // condition is true }1 if(condition) { // Statements to execute if // condition is true }2 if (condition1) { // Executes when condition1 is true if (condition2) { // Executes when condition2 is true } }3 if(condition) { // Statements to execute if // condition is true }2 if(condition) { // Statements to execute if // condition is true }5 if(condition) { // Statements to execute if // condition is true }6
if(condition) { // Statements to execute if // condition is true }2 if(condition) { // Statements to execute if // condition is true }9 if (condition1) { // Executes when condition1 is true if (condition2) { // Executes when condition2 is true } }9 i is less than 154 if(condition) statement1; statement2; // Here if the condition is true, if block // will consider only statement1 to be inside // its block.9 i is smaller than 15 i is smaller than 12 too2 if(condition) statement1; statement2; // Here if the condition is true, if block // will consider only statement1 to be inside // its block.2 if(condition) { // Statements to execute if // condition is true }2 i is smaller than 15 i is smaller than 12 too5 i is less than 154 if(condition) statement1; statement2; // Here if the condition is true, if block // will consider only statement1 to be inside // its block.9 if (condition) { // Executes this block if // condition is true } else { // Executes this block if // condition is false }0 if(condition) statement1; statement2; // Here if the condition is true, if block // will consider only statement1 to be inside // its block.2 if (condition) { // Executes this block if // condition is true } else { // Executes this block if // condition is false }2 Keluaran i is less than 15 Pernyataan nested-if JavaScript. JavaScript memungkinkan kita untuk menyarangkan pernyataan if di dalam pernyataan if. saya. e, kita dapat menempatkan pernyataan if di dalam pernyataan if lainnya. Nested if adalah pernyataan if yang merupakan target dari if or else lainnya. Sintaksis if (condition1) { // Executes when condition1 is true if (condition2) { // Executes when condition2 is true } } Flow chart pernyataan nested-if Contoh. Contoh ini menjelaskan pernyataan nested-if di Javascript JavaScript
if(condition) { // Statements to execute if // condition is true }_0 if(condition) { // Statements to execute if // condition is true }1 if(condition) { // Statements to execute if // condition is true }2 if (condition) statement; else if (condition) statement; . . else statement;5 if(condition) { // Statements to execute if // condition is true }2 if(condition) { // Statements to execute if // condition is true }5 if(condition) { // Statements to execute if // condition is true }6 if(condition) { // Statements to execute if // condition is true }_2 if(condition) { // Statements to execute if // condition is true }2 if(condition) { // Statements to execute if // condition is true }9 i is 202 i is 203 i is 204 if(condition) { // Statements to execute if // condition is true }9 i is 206 i is 207 if(condition) statement1; statement2; // Here if the condition is true, if block // will consider only statement1 to be inside // its block.9 i is 209 if(condition) statement1; statement2; // Here if the condition is true, if block // will consider only statement1 to be inside // its block.2 i is 207 <script type = 2i is 207 <script type = 4i is 207 <script type = 6i is 207 if(condition) { // Statements to execute if // condition is true }9 <script type = 9if(condition) { // Statements to execute if // condition is true }00 if(condition) statement1; statement2; // Here if the condition is true, if block // will consider only statement1 to be inside // its block.9 if(condition) { // Statements to execute if // condition is true }02 if(condition) statement1; statement2; // Here if the condition is true, if block // will consider only statement1 to be inside // its block.2 i is 207 i is smaller than 15 i is smaller than 12 too5 if(condition) { // Statements to execute if // condition is true }00 if(condition) statement1; statement2; // Here if the condition is true, if block // will consider only statement1 to be inside // its block.9 if(condition) { // Statements to execute if // condition is true }08 if(condition) statement1; statement2; // Here if the condition is true, if block // will consider only statement1 to be inside // its block.2 if(condition) { // Statements to execute if // condition is true }10 if(condition) { // Statements to execute if // condition is true }11 if(condition) { // Statements to execute if // condition is true }2 if(condition) { // Statements to execute if // condition is true }11 if (condition) { // Executes this block if // condition is true } else { // Executes this block if // condition is false }2 Keluaran i is smaller than 15 i is smaller than 12 too Pernyataan JavaScript if-else-if ladder. Di sini, pengguna dapat memutuskan di antara beberapa opsi. Pernyataan if dijalankan dari atas ke bawah. Segera setelah salah satu kondisi yang mengontrol if benar, pernyataan yang terkait dengan if dijalankan, dan tangga lainnya dilewati. Jika tidak ada kondisi yang benar, maka pernyataan akhir yang lain akan dieksekusi Bisakah Anda menggunakan array dalam pernyataan if?ArrayFormula dengan fungsi IF
. Anda dapat melakukannya dengan menetapkan rumus IF sebagai ArrayFormula. You can use ranges (arrays) with non-array functions. You can do this by designating the IF formula as an ArrayFormula.
Bagaimana cara memeriksa kondisi dalam array JavaScript?Anda dapat menggunakan metode include() dalam JavaScript untuk memeriksa apakah suatu item ada dalam array. Anda juga dapat menggunakannya untuk memeriksa apakah ada substring di dalam string. Mengembalikan nilai true jika item ditemukan dalam array/string dan false jika item tidak ada.
Bagaimana cara memeriksa apakah kondisi untuk array berisi nilai dalam JavaScript?JavaScript Array includes()
. Metode include() mengembalikan false jika nilainya tidak ditemukan.
Bagaimana Anda menulis kondisi array?Cara termudah dan paling jelas adalah mendefinisikan larik dengan semua elemen yang diperlukan, lalu memanggil push dengan syarat . constArrayKu = ['a', 'b', 'c', 'd']; . |