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
Related key terms
1 of 5
4 Key excerpts on "Javascript Logical Operators"
- eBook - ePub
Clean Code in JavaScript
Develop reliable, maintainable, and robust JavaScript
- James Padolsey(Author)
- 2020(Publication Date)
- Packt Publishing(Publisher)
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:- Cast the operand to a Boolean (Boolean(operand))
- 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), - No longer available |Learn more
- Ved Antani(Author)
- 2016(Publication Date)
- Packt Publishing(Publisher)
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.There are three Boolean operators in JavaScript—AND(&), OR(|), and NOT(!).Boolean operators
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 falseHowever, 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.
- 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
- eBook - ePub
- 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 - No longer available |Learn more
Multimedia Web Design and Development
Using Languages to Build Dynamic Web Pages
- Theodor Richardson, Charles Thies(Authors)
- 2012(Publication Date)
- Mercury Learning and Information(Publisher)
switch is a conditional branch that compares a value to preset results to determine which path to execute.An if statement evaluates its argument to see if it is true or false. The argument can be any variable that contains a value of true or false, a conditional expression, or a combination of values that logically evaluates to true or false. The basic structure of an if statement in JavaScript is:For conditional expressions, the following symbols can be used to evaluate comparisons of variables, functions, and literal values:• > will evaluate to true if the left side of the statement is greater than the right side; an example of this is (x > y)• >= will evaluate to true if the left side of the statement is greater than or equal to the right side ; an example of this is (x >= y)• < will evaluate to true if the left side of the statement is less than the right side; an example of this is (x < y)• <= will evaluate to true if the left side of the statement is less than or equal to the right side; an example of this is (x <= y)• == will evaluate to true only if both sides of the statement are equal; an example of this is (x == y)• != will evaluate to true only if both sides of the statement are not equal; an example of this is (x != y)Multiple values can be concatenated with logical operations for the conditional statement as well:• || represents an OR condition, which evaluates to true if either side of the statement is true; an example of this is (x|| y)• && represents an AND condition, which evaluates to true only if both sides of the statement are true; an example of this is (x && y)Care must be taken to group chains of these statements correctly so that no more than two arguments are present for each of the concatenated elements. A statement can also be inverted using the NOT operator, which is represented by an exclamation point before the value; an example of this is !(x) which would switch the boolean value of x
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.



