How to write an inline IF statement in JavaScript?

WBOY
Release: 2023-09-07 08:01:05
forward
1101 people have browsed it

如何在 JavaScript 中编写内联 IF 语句?

Conditional statements are the most important and basic concepts in any programming language. The if-else statement allows us to conditionally execute any block of code. We can define the condition of the if statement in curly brackets. If the condition is true, the code of the if block is executed; otherwise, it executes the code of the else block.

Here, we demonstrate how if-else statements work in JavaScript.

if (condition) { // code to execute when the condition becomes true } else { // code to execute when the condition becomes false }
Copy after login

From the above code, users can understand the syntax of if-else statements.

What if I said you could write the above five lines of code into one line? Yes, you can do this using an inline if statement.

grammar

Users can use inline if statements in JavaScript according to the following syntax.

Condition? code - block - 1 : code - block - 2
Copy after login

In the above syntax, the condition is an expression. When the conditional expression is true, code block 1 is executed; otherwise, it executes code block 2.

If we compare the inline if statement with the if-else statement, then code-block-1 is the code of the if statement and code-block-2 is the code of the else statement.

Example

In the following example, we will learn the basic usage of inline if statements. We have used the condition "10===10", if the condition is true, it will print "10 is not equal to 10"; otherwise, it will print "10 is not equal to 10".

In the output, the user can observe that it prints "10 is equal to 10" since the condition always evaluates to true.

  

Using the inline if statement in JavaScript

Copy after login

Example

In the example below, we create an array of numbers. Additionally, we have created func1() and func2() functions that print different messages using the values passed as parameters.

We use the forEach() method to loop through the array. In the callback function of the forEach() method, we check whether the number is divisible by 10, then call the func1() function; otherwise, call the func2() function.

  

Using the inline if statement in JavaScript

Copy after login

Example

In the following example, we use if-else statement and inline if statement to check if the year is a leap year. The checkYear() function uses an if-else statement to ensure whether the year passed as an argument is a leap year.

In the checkInlineYear() function, we implement the same logic as in the checkYear() function, but we convert the if-else statement into an inline if statement. The user can see how we wrote nine lines in one line.

Users can observe that both functions give the same output for any year value.

  

Using inline if statement to check whether year is leap year in JavaScript

Copy after login

Users learned to use inline if statements in JavaScript. We can observe that inline if statements make the code cleaner and more readable and it is always good to write less lines of code with the same logic.

The above is the detailed content of How to write an inline IF statement in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!