JavaScript logical operators (operand operators)

Source: Internet
Author: User
Tags logical operators

1. Overview

|| (or) and && (and) are logical operators. But or/with the "logical operator" is not appropriate, called " operand operator " more appropriate!

Because | | (or) and && (and) are not Boolean values, but one (and only one) of the two operands.

2. Which operand is returned?

(1) a| | B

a| | b equivalent to a?a:b

(2) A&&b

A&&b equivalent to a?b:a

3. Application

(1) | | App--Set default values

<!DOCTYPE HTML><HTMLLang= "zh">    <Head>        <MetaCharSet= "UTF-8" />        <title>|| Application</title>    </Head>    <Body>        <Scripttype= "Text/javascript">            functionFoo (A, b) {a=a|| 'Hello'; b=b|| ' World'; Console.log (A+ ' ' +b); }        </Script>    </Body></HTML>

(2) && application--"Guardian operator"

<!DOCTYPE HTML><HTMLLang= "zh">    <Head>        <MetaCharSet= "UTF-8" />        <title>&& applications</title>    </Head>    <Body>        <Scripttype= "Text/javascript">            functionFoo (a) {Console.log (a); }            vara= 3; //Foo is only called when a condition is determined to be truea&&foo (); </Script>    </Body></HTML>

JavaScript logical operators (operand operators)

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.