Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the conditional operator for javascript

Shulou Source: shulou.com Published: 2022-06-01 16:43:13 09月14日 Update

Today Xiaobian to share with you what the conditional operators of javascript is related knowledge points, detailed content, clear logic, I believe most people still know too much about this knowledge, so share this article for everyone to refer to, I hope you read this article after harvest, let's learn about it together.

Javascript has conditional operators. In js, conditional operators are also called "ternary operators" and the syntax is "conditional expression?" Expression 1 : Expression 2 ;"; If the result of Conditional Expression is true, execute the code in Expression 1, otherwise execute the code in Expression 2.

Operating environment of this tutorial: Windows 7 system, Javascript version 1.8.5, Dell G3 computer.

Conditional operators in javascript

In javascript, conditional operators, also known as ternary operators, are the only operators that use three operands, consisting of a question mark and a colon. The syntax is as follows:

conditional expression? Expression 1 : Expression 2 ;

If the result of Conditional Expression is true, execute the code in Expression 1, otherwise execute the code in Expression 2.

execution flow

Conditional operators, when executed, first evaluate the conditional expression

If this value is true, expression 1 is executed

If the value is false, expression 2 is executed

Examples:

var a = 10;var b = 20;a > b ? alert("statement 1"):alert("statement 2")

The above is "javascript conditional operator is what" all the content of this article, thank you for reading! I believe everyone has a great harvest after reading this article. Xiaobian will update different knowledge for everyone every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Expressions conditions operators operations code knowledge articles content results statements syntax ternary different very large three colons most that is operands tutorials Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Linux macOS Shulou Tech Info MariaDB