javascript - javascript logic - javascript operators

Comparing Values by Using Operators: Javascript AND, Javascript OR, and Many More

Comparing one value to another is an important programming technique that developers use to create logical flows. Javascript provides many ways to compare simple values using language constructs called “operators.”

The Javascript standard defines two different types of simple operators.

  • Logical Operators — logical relationships between values
  • Comparison Operators — how values are related to each other

These operators work with Javascript data types that hold simple values, like strings or numbers, but cannot handle complex values like Javascript objects, arrays, or functions. To compare complex objects, developers must write their own comparison functions.

Make sure that the compared values or variables are the same data type; otherwise you may get an answer you don’t expect. Each operator type has different rules for valid comparisons, which the next sections will cover in more detail.

Javascript Logical Operators: AND, OR, and NOT

Javascript evaluates logical operators from left to right in the order they appear. Logical operators always evaluate true and false values. Changing the order of evaluation through grouping with parentheses often changes the final logical result. Assuming 4 different values compared by logical operators (denoted by the generic “op”), each of these four configurations have a different evaluation process.

A op B op C op D    // Evaluate in order: A, B, C, D
A op (B op C) op D  // Evaluate B/C, then A/BC, then ABC/D
A op B op (C op D)  // Evaluate C/D, then A, B, C/D
A op (B op C op D)  // Evaluate B, C, D, then A/BCD

Javascript AND Operator

The Javascript AND operator returns true if both arguments evaluate to true. Otherwise, it returns false. The Javascript AND operator is two ampersands (&&).

let a = true;
let b = true;
let c = false;
let d = false;

let test1 = (a && b);      // true
let test2 = (a && c);      // false
let test3 = (c && d);      // false
let test4 = (a && b && c); // false

Javascript OR Operator

The Javascript OR operator returns true if one of its arguments evaluates to true. If both of its arguments are false, it returns false. The Javascript OR operator is two pipes (||). Make sure to use the proper character, since pipes look similar to a capital letter “I” or lowercase letter “L” in some typefaces.

let a = true;
let b = true;
let c = false;
let d = false;

let test1 = (a || b);      // true
let test2 = (a || c);      // true
let test3 = (c || d);      // false
let test4 = (a || b || c); // false
let test5 = (c || a || b); // true

Javascript NOT Operator

The Javascript NOT operator negates its argument. To negate a single value, place the not operator directly in front of the argument. To negate the result of a comparison, enclose the comparison in parentheses, then put the operator directly in front of the parentheses. The Javascript NOT operator is an exclamation point (!).

let a = true;
let b = true;
let c = false;
let d = false;

let test1 = !a && b;        // false
let test2 = !(a && b);      // false
let test3 = !(c || d);      // true
let test4 = a || !(b || c); // true
let test5 = !(c || a || b); // false

Javascript Comparison Operators: Greater, Less, or Equal

The Javascript comparison operators evaluate quantitative and qualitative values. They determine the relative status of values, whether values are undefined, or whether values are identical. Avoid comparing values of different types, such as a string and a number, because their results may be unintuitive.

Javascript can compare whether one string is larger than another using “lexicographic order,” comparing strings letter by letter. Never compare a numeric string to a number because lexicographic order might cause unexpected results.

Javascript Greater Than Operator

The Javascript greater than operator (>) determines whether one value is larger than another.


let x = 10
x > 8           // true
>x > "8"        // Incorrect, don't use a string.

let y = "abc";
let z = "def";
y > z;          // false
z > y;          // true

Javascript Less Than Operator

The Javascript less than operator (<) determines whether one value is smaller than another.

let x = 10;
x < 8      // false
x < "8"    // Incorrect, don't use a string.

let y = "abc";
let z = "def";

y < z;     // true
z < y;     // false

Javascript Equals Operators

Javascript has two different operators that determine whether values are equal. The non-strict equals operator (==) determines if two values are effectively equal regardless of their data type. The strict equals operator (===) determines if two values are exactly the same in both data type and value.

let x = 1;
x == 1;     // true, non-strict comparison
x == "1";   // true, non-strict comparison
x === 1;    // true, strict comparison
x === "1";  // false, strict comparison

Javascript Not Equals Operators

Javascript also has two different operators that determine whether values are not equal. They look like the equals operators, except that they also contain an exclamation point in front of the equals signs. They make comparisons in the same way as their non-strict and strict equals counterparts.

let x = 2;
x !== 2;      // false, non-strict comparison
x !== "2";    // false, non-strict comparison
x !=== 2;     // false, strict comparison
x !=== "2";   // true, strict comparison

Javascript Greater Than or Equals Operator

The Javascript greater than or equals operator (>=) determines whether one value is larger than or equal to another.

let x = 10;
x >= 8;      // true
x >= "8";    // Incorrect, don't use a string.

let y = "abc";
let yy = "abc";
let z = "def";

y >= z;      // false
y >= yy;     // true
z >= y;      // true

Javascript Less Than or Equals Operator

The Javascript greater than or equals operator (<=) determines whether one value is smaller than or equal to another.

let x = 10;
x <= 8;          // false
x <= "8";        // Incorrect, don't use a string.

let y = "abc";
let yy = "abc";
let z = "def";

y <= z;         // true
y <= yy;        // true
z <= y;         // false

Conclusion

Javascript’s logical and comparison operators offer great flexibility in how developers can examine the relationships between values and variables. They are vital to proper function of many Javascript code structures, including switch statements and if else statements.

Enroll in our Intro to Programming Nanodegree Program today to learn more about Javascript operators and other programming concepts.

Start Learning

Jessica Reuter Castrogiovanni
Jessica Reuter Castrogiovanni