Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators

The basics of JavaScript Operator you need to know

Updated
•6 min read•View as Markdown
JavaScript Operators
S
Software Developer | Full Stack Developer |

What are JavaScript Operators?

JavaScript operators are symbols or keywords used to perform operations on values and variables. They are essential for manipulating data, making comparisons, assigning values, and controlling program flow.

Arithmetic Operators

  1. Addition (+) -> The addition (+) operator produces the sum of numeric operands or string concatenation.
console.log(2 + 2);
// Expected output: 4

console.log(2 + true);
// Expected output: 3

console.log("hello " + "everyone");
// Expected output: "hello everyone"

console.log(2001 + ": A Space Odyssey");
// Expected output: "2001: A Space Odyssey"
  1. Subtraction (-) -> The subtraction (-) operator subtracts the two operands, producing their difference.
console.log(5 - 3);
// Expected output: 2

console.log(3.5 - 5);
// Expected output: -1.5

console.log(5 - "hello");
// Expected output: NaN

console.log(5 - true);
// Expected output: 4
  1. Multiplication (*) -> The multiplication (*) operator produces the product of the operands.
console.log(3 * 4);
// Expected output: 12

console.log(-3 * 4);
// Expected output: -12

console.log("3" * 2);
// Expected output: 6

console.log("foo" * 2);
// Expected output: NaN
  1. Division (/) -> The division (/) operator produces the quotient of its operands where the left operand is the dividend and the right operand is the divisor.
console.log(12 / 2);
// Expected output: 6

console.log(3 / 2);
// Expected output: 1.5

console.log(6 / "3");
// Expected output: 2

console.log(2 / 0);
// Expected output: Infinity
  1. Remainder (%) -> The remainder (%) operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend.
console.log(13 % 5);
// Expected output: 3

console.log(-13 % 5);
// Expected output: -3

console.log(4 % 2);
// Expected output: 0

console.log(-4 % 2);
// Expected output: -0

Comparison Operators

  1. Equality (==) -> The equality (==) operator checks whether its two operands are equal, returning a Boolean result. Unlike the strict equality (===) operator, it attempts to convert and compare operands that are of different types.
console.log(1 == 1);
// Expected output: true

console.log("hello" == "hello");
// Expected output: true

console.log("1" == 1);
// Expected output: true

console.log(0 == false);
// Expected output: true
  1. Strict Equality (===) -> The strict equality (===) operator checks whether its two operands are equal, returning a Boolean result. Unlike the equality (==) operator, the strict equality operator always considers operands of different types to be different.
console.log(1 === 1);
// Expected output: true

console.log("hello" === "hello");
// Expected output: true

console.log("1" === 1);
// Expected output: false

console.log(0 === false);
// Expected output: false
  1. Inequality (!=) -> The inequality (!=) operator checks whether its two operands are not equal, returning a Boolean result. Unlike the strict inequality (!==) operator, it attempts to convert and compare operands that are of different types.
console.log(1 != 1);
// Expected output: false

console.log("hello" != "hello");
// Expected output: false

console.log("1" != 1);
// Expected output: false

console.log(0 != false);
// Expected output: false
  1. Strict Inequality (!==) -> The strict inequality (!==) operator checks whether its two operands are not equal, returning a Boolean result. Unlike the inequality (!=) operator, the strict inequality operator always considers operands of different types to be different.
console.log(1 !== 1);
// Expected output: false

console.log("hello" !== "hello");
// Expected output: false

console.log("1" !== 1);
// Expected output: true

console.log(0 !== false);
// Expected output: true
  1. Greater Than (>) -> The greater than (>) operator returns true if the left operand is greater than the right operand, and false otherwise.
console.log(5 > 3);
// Expected output: true

console.log(3 > 3);
// Expected output: false

// Compare bigint to number
console.log(3n > 5);
// Expected output: false

console.log("ab" > "aa");
// Expected output: true
  1. Lesser Than (<) -> The less than (<) operator returns true if the left operand is less than the right operand, and false otherwise.
console.log(5 < 3);
// Expected output: false

console.log(3 < 3);
// Expected output: false

// Compare bigint to number
console.log(3n < 5);
// Expected output: true

console.log("aa" < "ab");
// Expected output: true

Logical Operators

  1. Logical AND (&&) -> The logical AND (&&) (logical conjunction) operator for a set of boolean operands will be true if and only if all the operands are true. Otherwise it will be false. The operator returns the value of the first falsy operand encountered when evaluating from left to right, or the value of the last operand if they are all truthy.
const a = 3;
const b = -2;

console.log(a > 0 && b > 0);
// Expected output: false
  1. Logical OR (||) -> The logical OR (||) (logical disjunction) operator for a set of operands is true if and only if one or more of its operands is true. It is typically used with boolean (logical) values. When it is, it returns a Boolean value. However, the || operator actually returns the value of one of the specified operands, so if this operator is used with non-Boolean values, it will return a non-Boolean value.
const c = 3;
const d = -2;

console.log(c > 0 || d > 0);
// Expected output: true
  1. Logical NOT (!) -> The logical NOT (!) (logical complement, negation) operator takes truth to falsity and vice versa. It is typically used with boolean (logical) values. When used with non-Boolean values, it returns false if its single operand can be converted to true; otherwise, returns true.
const e = 3;
const f = -2;

console.log(!(e > 0 || f > 0));
// Expected output: false

Assignment Operators

  1. Assignment (=) -> The assignment (=) operator is used to assign a value to a variable or property. The assignment expression itself has a value, which is the assigned value. This allows multiple assignments to be chained in order to assign a single value to multiple variables.
let x = 2;
const y = 3;

console.log(x);
// Expected output: 2

console.log((x = y + 1)); // 3 + 1
// Expected output: 4

console.log((x = x * y)); // 4 * 3
// Expected output: 12
  1. Addition Assignment (+=) -> The addition assignment (+=) operator performs addition (which is either numeric addition or string concatenation) on the two operands and assigns the result to the left operand.
let g = 2;
let h = "hello";

console.log((g += 3)); // Addition
// Expected output: 5

console.log((h += " world")); // Concatenation
// Expected output: "hello world"
  1. Subtraction Assignment (-=) -> The subtraction assignment (-=) operator performs subtraction on the two operands and assigns the result to the left operand.
let m = 2;

console.log((m -= 3));
// Expected output: -1

console.log((m -= "Hello"));
// Expected output: NaN