Computer Science

Javascript Logical Operators

Javascript logical operators are used to combine two or more conditions to produce a single true or false value. The three logical operators are AND (&&), OR (||), and NOT (!). They are commonly used in conditional statements and loops to control program flow.

Written by Perlego with AI-assistance

3 Key excerpts on "Javascript Logical Operators"

  • Clean Code in JavaScript
    eBook - ePub

    Clean Code in JavaScript

    Develop reliable, maintainable, and robust JavaScript

    The unary minus operator is usually only used directly with a literal number operand to specify a negative value. As with all other arithmetic operators, we should ensure that our intent is clear and that we are not confusing people with long or confusing expressions.
    Now that we've explored arithmetic operators, we can begin to look into logical operators. Passage contains an image

    Logical operators

    Logical operators are typically used to build logical expressions where the result of the expression informs some action or inaction. There are three logical operators in JavaScript:
    • The NOT operator ( !a)
    • The AND operator (a && b)
    • The OR operator (a || b)
    As with most other operators, they can accept a variety of types and will coerce as necessary. The AND and OR operators, unusually, do not always evaluate to a Boolean value, and both utilize a mechanism called short-circuit evaluation to only execute both operands if some condition is met. We'll learn more about this as we explore each individual logical operator.
    Passage contains an image

    The logical NOT operator

    The NOT operator is a unary operator. It accepts only a single operand and converts that operand into its Boolean representation, then inverts it, so that truthy items become false and falsy items become true:
    !1; // => false!true; // => false!'hi; // => false!0; // => true!''; // => true!true; // => false Internally, the NOT operator will perform the following:
    1. Cast the operand to a Boolean (Boolean(operand))
    2. If the resulting value is true, then return false; otherwise, return true
    As discussed in the Conversion to a Boolean section in the last chapter, a typical idiom for converting a value to its Boolean representation is the double NOT (that is, !!value) as this effectively reverses the truthiness or falsiness of the value twice and evaluates to a Boolean. The more explicit and slightly more preferred idiom is to use Boolean(value),
  • Mastering JavaScript
    b variable is created as an accidental global. (If you are in the strict mode, you will get an error for this.) With JavaScript, be careful what you wish for, you might get it.

    Boolean operators

    There are three Boolean operators in JavaScript—AND(&), OR(|), and NOT(!).
    Before we discuss logical AND and OR operators, we need to understand how they produce a Boolean result. Logical operators are evaluated from left to right and they are tested using the following short-circuit rules:
    • Logical AND : If the first operand determines the result, the second operand is not evaluated.In the following example, I have highlighted the right-hand side expression if it gets executed as part of short-circuit evaluation rules:
      console.log(true && true ); // true AND true returns true console.log(true && false );// true AND false returns false console.log(false && true);// false AND true returns false console.log("Foo" && "Bar" );// Foo(true) AND Bar(true) returns Bar console.log(false && "Foo");// false && Foo(true) returns false console.log("Foo" && false );// Foo(true) && false returns false console.log(false && (1 == 2));// false && false(1==2) returns false
    • Logical OR : If the first operand is true, the second operand is not evaluated:
      console.log(true || true); // true AND true returns true console.log(true || false);// true AND false returns true console.log(false || true );// false AND true returns true console.log("Foo" || "Bar" );// Foo(true) AND Bar(true) returns Foo console.log(false || "Foo" );// false && Foo(true) returns Foo console.log("Foo" || false);// Foo(true) && false returns Foo console.log(false || (1 == 2));// false && false(1==2) returns false
      However, both logical AND and logical OR can also be used for non-Boolean operands. When either the left or right operand is not a primitive Boolean value, AND and OR do not return Boolean values.
    Now we will explain the three logical Boolean operators:
    • Logical AND(&&): If the first operand object is falsy , it returns that object. If its truthy , the second operand object is returned:console.log (0 && "Foo"); //First operand is falsy - return it console.log ("Foo" && "Bar"); //First operand is truthy, return the second operand
  • Beginning HTML and CSS
    • Rob Larsen(Author)
    • 2013(Publication Date)
    • Wrox
      (Publisher)
    true and y>1 is true . So the following expression returns true because both of the operands evaluate to true . (You can see more examples by referring to the right column in Table 10-4 .)
    (x<2 && y>1);

    String Operators (Using + with Strings)

    You can also add text to strings using the + operator. For example, here the + operator is used to add two variables that are strings together:
    var firstName = "Bob "; var lastName = "Stewart"; name = firstName + lastName;
    The value of the name variable would now be Bob Stewart . The process of adding two strings together is known as concatenation .
    You can also compare strings using the comparison operators you just met. For example, you could check whether a user has entered a specific value into a textbox. (You see more about this topic when you look at the “Conditional Statements” section.)

    Functions

    A function is made up of related code that performs a particular task. For example, a function could be written to calculate the area given the width and height. The function can then be called elsewhere in the script or when an event fires.

    How to Define a Function

    There are three parts to creating or defining a function:
    • Define a name for it.
    • Indicate any values that might be required; these are known as arguments .
    • Add statements to the body of the function.
    For example, if you want to create a function to calculate the area of a rectangle, you might name the function calculateArea() . (A function name should be followed by parentheses.) To calculate the area, you need to know the rectangle’s width and height, so these would be passed in as arguments , which are the information the function needs to do its job. Inside the body of the function (the part between the curly braces) are the statements
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.