Bedingte JavaScript-Anweisungen: IF, Else, Else IF (Beispiel)

Inhaltsverzeichnis:

Anonim

In diesem Tutorial lernen Sie:

  • Verwendung von bedingten Anweisungen
  • Verschiedene Arten von bedingten Anweisungen
  • If-Anweisung
  • Wenn ... Sonst Aussage
  • If… Else If… Else Aussage

Verwendung von bedingten Anweisungen

Bedingte Anweisungen werden verwendet, um den Ausführungsfluss basierend auf verschiedenen Bedingungen zu bestimmen. Wenn eine Bedingung wahr ist, können Sie eine Aktion ausführen, und wenn die Bedingung falsch ist, können Sie eine andere Aktion ausführen.

Verschiedene Arten von bedingten Anweisungen

In JavaScript gibt es hauptsächlich drei Arten von bedingten Anweisungen.

  1. If-Anweisung
  2. Wenn ... Sonst Aussage
  3. If… Else If… Else Aussage

If-Anweisung

Syntax:

if (condition){lines of code to be executed if condition is true}

Sie können die If-Anweisung verwenden, wenn Sie nur eine bestimmte Bedingung überprüfen möchten.

Versuchen Sie dies selbst:

 IF-Anweisungen !!! </ title><script type = "text />var age = prompt ("Bitte geben Sie Ihr Alter ein");if (Alter> = 18)document.write ("Sie sind ein Erwachsener<br />");if (Alter <18)document.write ("Sie sind KEIN Erwachsener<br />");</ script></ head><body></ body></ html></pre> <a id="menu-2"></a>
<h2>Wenn ... Sonst Aussage</h2>
<p>Syntax:</p>

<div id="container-5533149ee6411a5f99b370c693e87966"></div>
<pre translate="no">if (condition){lines of code to be executed if the condition is true}else{lines of code to be executed if the condition is false}</pre>
<p>Sie können If verwenden</p>
… .Else-Anweisung, wenn Sie zwei Bedingungen überprüfen und einen anderen Satz von Codes ausführen müssen.
<p>Versuchen Sie dies selbst:</p>
<pre translate="no" class="codeguru"><html><head><title> If… Else Statments !!! </ title><script type = "text />// Aktuelle Stunden abrufenvar Stunden = neues Datum (). getHours ();if (Stunden <12)document.write ("Guten Morgen !!!<br />");sonstdocument.write ("Guten Tag !!!<br />");</ script></ head><body></ body></ html></pre> <a id="menu-3"></a>
<h2>If… Else If… Else Aussage</h2>
<p>Syntax:</p>
<pre translate="no">if (condition1){lines of code to be executed if condition1 is true}else if(condition2){lines of code to be executed if condition2 is true}else{lines of code to be executed if condition1 is false and condition2 is false}</pre>
<p>Sie können If verwenden</p>
… .Else If… .Else-Anweisung, wenn Sie mehr als zwei Bedingungen überprüfen möchten.
<p>Versuchen Sie dies selbst:</p>
<pre translate="no" class="codeguru"><html><head><script type = "text />var one = prompt ("Geben Sie die erste Nummer ein");var two = prompt ("Geben Sie die zweite Nummer ein");eins = parseInt (eins);zwei = parseInt (zwei);if (eins == zwei)document.write (eins + "ist gleich" + zwei + ".");sonst wenn (eins <zwei)document.write (eins + "ist kleiner als" + zwei + ".");sonstdocument.write (eins + "ist größer als" + zwei + ".");</ script></ head><body></ body></ html></pre> <form class="hidden" code-box" name="sample"> Dieser Code kann bearbeitet werden. Klicken Sie zum Ausführen auf Ausführen<textarea name="codeguru"> </textarea>
<p><input type="submit" value="Lauf" style="background-color:#df5035;" color:#ffffff;"></p>


										</div>
									</article>
									
<div id="container-5533149ee6411a5f99b370c693e87966"></div>								</div>
							</div>
						</div>
					</div>
				</div>
				
<aside class="col-lg-4 sidebar sidebar--right">
	<div class="widget widget-popular-posts">
		<h2 class="widget-title">Beliebte Beiträge</h2>
		<ul class="post-list-small">
			<li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__img-holder">
						<div class="thumb-container thumb-100"><a href="/8003001-min-height" title="Min-Höhe - CSS-Tricks" rel="bookmark"><img src="https://cdn.css-code.org/4556727/min-height_css-tricks.png.webp" loading="lazy" alt="Min-Höhe - CSS-Tricks" title="Min-Höhe - CSS-Tricks" class="post-list-small__img--rounded" style="width:88px;height:88px" /></a></div>
					</div>
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8003001-min-height" title="Min-Höhe - CSS-Tricks" rel="bookmark">Min-Höhe - CSS-Tricks 2025</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__img-holder">
						<div class="thumb-container thumb-100"><a href="/8003002-opacity" title="Deckkraft - CSS-Tricks" rel="bookmark"><img src="https://cdn.css-code.org/7207130/opacity_css-tricks.png.webp" loading="lazy" alt="Deckkraft - CSS-Tricks" title="Deckkraft - CSS-Tricks" class="post-list-small__img--rounded" style="width:88px;height:88px" /></a></div>
					</div>
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8003002-opacity" title="Deckkraft - CSS-Tricks" rel="bookmark">Deckkraft - CSS-Tricks 2025</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__img-holder">
						<div class="thumb-container thumb-100"><a href="/8003003-order" title="Bestellen - CSS-Tricks" rel="bookmark"><img src="https://cdn.css-code.org/3520315/order_css-tricks.png.webp" loading="lazy" alt="Bestellen - CSS-Tricks" title="Bestellen - CSS-Tricks" class="post-list-small__img--rounded" style="width:88px;height:88px" /></a></div>
					</div>
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8003003-order" title="Bestellen - CSS-Tricks" rel="bookmark">Bestellen - CSS-Tricks 2025</a></h3>
					</div>
				</div>
			</li>
		</ul>
	</div>

	<div class="widget widget-popular-posts">
		<h2 class="widget-title">Die besten Bewertungen für den Monat</h2>
		<ul class="post-list-small">
			<li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8225643-wsdl-tutorial-web-services-description-language-with-example" title="WSDL-Lernprogramm: Web Services-Beschreibungssprache mit Beispiel" rel="bookmark">WSDL-Lernprogramm: Web Services-Beschreibungssprache mit Beispiel</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8225644-web-servicews-security-tutorial-with-soap-example" title="Web Service (WS) Sicherheits-Tutorial mit SOAP-Beispiel" rel="bookmark">Web Service (WS) Sicherheits-Tutorial mit SOAP-Beispiel</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8225645-soap-vs-rest-difference-between-web-api-services" title="SOAP Vs. REST: Unterschied zwischen Web-API-Diensten" rel="bookmark">SOAP Vs. REST: Unterschied zwischen Web-API-Diensten</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8225646-soa-service-oriented-architecture-principles" title="SOA-Prinzipien (Service Oriented Architecture)" rel="bookmark">SOA-Prinzipien (Service Oriented Architecture)</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8225647-restful-web-services-tutorial-with-rest-api-example" title="RESTful Web Services Tutorial mit REST API Beispiel" rel="bookmark">RESTful Web Services Tutorial mit REST API Beispiel</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8225648-api-vs-web-service-whats-the-difference" title="API vs Web Service: Was ist der Unterschied?" rel="bookmark">API vs Web Service: Was ist der Unterschied?</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8225649-json-vs-xml-whats-the-difference" title="JSON vs XML: Was ist der Unterschied?" rel="bookmark">JSON vs XML: Was ist der Unterschied?</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8225650-20-best-api-testing-tools-in-2021-rest-and-soap-web-services" title="20 besten API-Testtools im Jahr 2021: REST & SOAP-Webdienste" rel="bookmark">20 besten API-Testtools im Jahr 2021: REST & SOAP-Webdienste</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8225651-top-25-microservices-interview-questions-and-answers" title="Top 25 Fragen und Antworten zum Interview mit Microservices" rel="bookmark">Top 25 Fragen und Antworten zum Interview mit Microservices</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8225652-what-is-itsm-it-service-management-processes-framework-benefits" title="Was ist ITSM? IT Service Management Prozesse, Framework, Vorteile" rel="bookmark">Was ist ITSM? IT Service Management Prozesse, Framework, Vorteile</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8225653-top-70-web-services-interview-questions-and-answers" title="Top 70 Fragen zum Web Services-Interview & Antworten" rel="bookmark">Top 70 Fragen zum Web Services-Interview & Antworten</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8225654-10-best-service-virtualization-tools-in-2021-microservices-and-mocking" title="10 besten Service-Virtualisierungstools im Jahr 2021: Microservices und Mocking" rel="bookmark">10 besten Service-Virtualisierungstools im Jahr 2021: Microservices und Mocking</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8225655-exporting-data-from-r-how-to-export-data-from-r-to-csv-excel" title="Exportieren von Daten aus R: So exportieren Sie Daten aus R nach CSV, Excel" rel="bookmark">Exportieren von Daten aus R: So exportieren Sie Daten aus R nach CSV, Excel</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8225656-top-50-devops-interview-questions-and-answers" title="Top 50 DevOps Interview Fragen & Antworten" rel="bookmark">Top 50 DevOps Interview Fragen & Antworten</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8225657-best-monitor-for-programming-and-coding-2021-update" title="Bester Monitor für die Programmierung & Codierung (2021 Update)" rel="bookmark">Bester Monitor für die Programmierung & Codierung (2021 Update)</a></h3>
					</div>
				</div>
			</li>
		</ul>
	</div>
	
<div id="container-5533149ee6411a5f99b370c693e87966"></div>	<div class="widget widget-popular-posts">
		<h2 class="widget-title">Top Artikel</h2>
		<ul class="post-list-small">
			<li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__img-holder">
						<div class="thumb-container thumb-100"><a href="/8226404-create-alter-and-drop-keyspace-in-cassandra-with-example" title="Erstellen, ändern & Legen Sie Keyspace in Cassandra mit Beispiel ab" rel="bookmark"><img src="https://cdn.css-code.org/1798776/create-_alter_ampamp_drop_keyspace_in_cassandra_with_example.png.webp" loading="lazy" alt="Erstellen, ändern & Legen Sie Keyspace in Cassandra mit Beispiel ab" title="Erstellen, ändern & Legen Sie Keyspace in Cassandra mit Beispiel ab" class="post-list-small__img--rounded" style="width:88px;height:88px" /></a></div>
					</div>
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8226404-create-alter-and-drop-keyspace-in-cassandra-with-example" title="Erstellen, ändern & Legen Sie Keyspace in Cassandra mit Beispiel ab" rel="bookmark">Erstellen, ändern & Legen Sie Keyspace in Cassandra mit Beispiel ab 2025</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__img-holder">
						<div class="thumb-container thumb-100"><a href="/8226405-cassandra-table-create-alter-drop-and-truncate-with-example" title="Cassandra-Tabelle: Erstellen, Ändern, Löschen & Abschneiden (mit Beispiel)" rel="bookmark"><img src="https://cdn.css-code.org/5643692/cassandra_table_create-_alter-_drop_ampamp_truncate_with_example.png.webp" loading="lazy" alt="Cassandra-Tabelle: Erstellen, Ändern, Löschen & Abschneiden (mit Beispiel)" title="Cassandra-Tabelle: Erstellen, Ändern, Löschen & Abschneiden (mit Beispiel)" class="post-list-small__img--rounded" style="width:88px;height:88px" /></a></div>
					</div>
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8226405-cassandra-table-create-alter-drop-and-truncate-with-example" title="Cassandra-Tabelle: Erstellen, Ändern, Löschen & Abschneiden (mit Beispiel)" rel="bookmark">Cassandra-Tabelle: Erstellen, Ändern, Löschen & Abschneiden (mit Beispiel) 2025</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__img-holder">
						<div class="thumb-container thumb-100"><a href="/8226406-top-15-big-data-tools-open-source-software-for-data-analytics" title="Top 15 Big Data Tools - Open Source Software für Data Analytics" rel="bookmark"><img src="https://cdn.css-code.org/3620710/top_15_big_data_tools_open_source_software_for_data_analytics.jpg.webp" loading="lazy" alt="Top 15 Big Data Tools - Open Source Software für Data Analytics" title="Top 15 Big Data Tools - Open Source Software für Data Analytics" class="post-list-small__img--rounded" style="width:88px;height:88px" /></a></div>
					</div>
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8226406-top-15-big-data-tools-open-source-software-for-data-analytics" title="Top 15 Big Data Tools - Open Source Software für Data Analytics" rel="bookmark">Top 15 Big Data Tools - Open Source Software für Data Analytics 2025</a></h3>
					</div>
				</div>
			</li>
		</ul>
	</div>
</aside>			</div>
		</div>
		
<footer class="footer footer--dark">
	<div class="container">
		<div class="footer__widgets">
			<div class="row">
				<div class="col-lg-4">
					<div class="widget">
						<h2 class="widget-title">Beliebte Beiträge</h2>
						<ul class="post-list-small">
							<li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8003801-check-if-element-exists" title="Überprüfen Sie, ob das Element vorhanden ist CSS-Tricks" rel="bookmark">Überprüfen Sie, ob das Element vorhanden ist CSS-Tricks</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8003802-check-if-event-was-triggered-or-native" title="Überprüfen Sie, ob das Ereignis ausgelöst oder nativ war CSS-Tricks" rel="bookmark">Überprüfen Sie, ob das Ereignis ausgelöst oder nativ war CSS-Tricks</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8003803-check-if-jquery-is-loaded" title="Überprüfen Sie, ob jQuery geladen ist CSS-Tricks" rel="bookmark">Überprüfen Sie, ob jQuery geladen ist CSS-Tricks</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8003804-clear-a-file-input" title="Dateieingabe löschen - CSS-Tricks" rel="bookmark">Dateieingabe löschen - CSS-Tricks</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8003806-clear-default-search-string-on-focus" title="Standard-Suchzeichenfolge im Fokus löschen - CSS-Tricks" rel="bookmark">Standard-Suchzeichenfolge im Fokus löschen - CSS-Tricks</a></h3>
									</div>
								</div>
							</li>
						</ul>
					</div>
				</div>
				<div class="col-lg-4">
					<div class="widget">
						<h2 class="widget-title">Tipp Der Redaktion</h2>
						<ul class="post-list-small">
						<li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8222806-how-to-integrate-uftqtp-with-alm-quality-center" title="So integrieren Sie UFT (QTP) in ALM (Quality Center)" rel="bookmark">So integrieren Sie UFT (QTP) in ALM (Quality Center)</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8222808-dashboard-reports-and-analysis-in-hp-alm-quality-center" title="Dashboard, Berichte & Analyse in HP ALM (Quality Center)" rel="bookmark">Dashboard, Berichte & Analyse in HP ALM (Quality Center)</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8222881-how-to-customize-project-in-hp-alm-quality-center" title="So passen Sie ein Projekt in HP ALM (Quality Center) an" rel="bookmark">So passen Sie ein Projekt in HP ALM (Quality Center) an</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8222883-hp-almqc-certification-definite-guide" title="HP ALM / QC-Zertifizierung: Definitiver Leitfaden" rel="bookmark">HP ALM / QC-Zertifizierung: Definitiver Leitfaden</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8222885-30-best-alm-tools-mar-2021-list" title="30 BEST ALM Tools (Liste März 2021)" rel="bookmark">30 BEST ALM Tools (Liste März 2021)</a></h3>
									</div>
								</div>
							</li>
						</ul>
					</div>
				</div>
				<div class="col-lg-4">
					<div class="widget">
						<h2 class="widget-title">Top Artikel</h2>
						<ul class="post-list-small">
						<li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8225129-postgresql-vs-mysql-whats-the-difference" title="PostgreSQL vs MySQL: Was ist der Unterschied?" rel="bookmark">PostgreSQL vs MySQL: Was ist der Unterschied?</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8225132-how-to-print-in-python-with-examples" title="Drucken in Python mit Beispielen" rel="bookmark">Drucken in Python mit Beispielen</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8225133-hello-world-create-your-first-python-program" title="Hallo Welt: Erstellen Sie Ihr erstes Python-Programm" rel="bookmark">Hallo Welt: Erstellen Sie Ihr erstes Python-Programm</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8225135-python-tuple-pack-unpack-compare-slicing-delete-key" title="Python TUPLE - Packen, Entpacken, Vergleichen, Schneiden, Löschen, Schlüssel" rel="bookmark">Python TUPLE - Packen, Entpacken, Vergleichen, Schneiden, Löschen, Schlüssel</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8225136-python-dictionary-append-how-to-add-keyvalue-pair" title="Python Dictionary Append: Hinzufügen eines Schlüssel / Wert-Paares" rel="bookmark">Python Dictionary Append: Hinzufügen eines Schlüssel / Wert-Paares</a></h3>
									</div>
								</div>
							</li>
						</ul>
					</div>
				</div>
			</div>
			<p>© Copyright de.css-code.org, 2025 September | 
				<a href="https://de.css-code.org/about-site" title="Über Site">Über Site</a> | 
				<a href="https://de.css-code.org/contacts" title="Kontakte">Kontakte</a> | 
				<a href="https://de.css-code.org/privacy-policy" title="Datenschutz-Bestimmungen">Datenschutz-Bestimmungen</a>.
			</p>
			
		</div>
	</div>
</footer>		
<link href="https://css-code.org/template/css/style.min.css" rel="preload" as="style" onload="this.onload=null;this.rel='stylesheet'" />	</body>
</html>