Conditional (triple-mesh) operator (?:)

Source: Internet
Author: User
Tags expression
condition | operation

Executes one of two statements according to a condition.

test ? Statement 1 : statement2

Parameters

Test

Any Boolean expression.

Statement 1

The statement to execute when test is true . Can be a compound statement .

Statement 2

The statement to execute when test is false . Can be a compound statement.

Description

?: operator is a shortcut to a if...else statement. It is often used as part of a larger expression, and it is not consistent to use the if...else statement here. For example:

?: " day.");

In this example, if it is 6pm, a string containing the "good evening." is created. The equivalent code for using the if...else statement is as follows:

var now = new Date();var greeting = "Good";if (now.getHours() > 17)greeting += " evening.";else   greeting += " day.";

Requirements

Version 1

Please see

If...else Statement | Operator Precedence | Operator Summary



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.