Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators Explained for Beginners

Updated
4 min readView as Markdown

In programming, we often need to perform operations on values.

Examples:

  • Add two numbers

  • Compare values

  • Check multiple conditions

  • Update a variable value

JavaScript provides operators to perform these actions.


What Are Operators?

An operator is a symbol used to perform operations on values or variables.

Example:

let result = 5 + 3;

Here:

  • 5 and 3 are operands

  • + is the operator

Output:

8

Types of Operators in JavaScript

Common categories of operators include:

Category Examples
Arithmetic + - * / %
Comparison == === != > <
Logical &&
Assignment = += -=

Arithmetic Operators

Arithmetic operators are used for basic mathematical calculations.

Operator Meaning Example
+ Addition 5 + 2
- Subtraction 5 - 2
* Multiplication 5 * 2
/ Division 10 / 2
% Modulus (remainder) 10 % 3

Example:

let a = 10;
let b = 3;

console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);
console.log(a % b);

Output

13
7
30
3.333
1

The % operator returns the remainder of a division.


Comparison Operators

Comparison operators are used to compare two values.

They return a boolean result (true or false).

Operator Meaning
== Equal to
=== Strict equal
!= Not equal
> Greater than
< Less than

Example:

let a = 10;
let b = 5;

console.log(a > b);
console.log(a < b);

Output

true
false

Difference Between == and ===

This is one of the most important concepts for beginners.

Example:

console.log(5 == "5");

Output

true

== converts values before comparing.

Now with strict comparison:

console.log(5 === "5");

Output

false

=== checks both value and type.

Operator Behavior
== compares values only
=== compares value and type

Because of this, developers usually prefer ===.


Logical Operators

Logical operators are used to combine multiple conditions.

Operator Meaning
&& AND
! NOT

Example:

let age = 20;
let hasID = true;

console.log(age >= 18 && hasID);

Output

true

Both conditions must be true for &&.


Logical Operator Truth Table

https://introcs.cs.princeton.edu/java/71boolean/images/truth-table.png https://cdn.hashnode.com/res/hashnode/image/upload/v1634698402709/M-rCqGBzA.png https://www.researchgate.net/publication/291418819/figure/fig3/AS%3A718510820962304%401548317737478/Summary-of-the-common-Boolean-logic-gates-with-symbols-and-truth-tables.png

4

Basic behavior:

| A | B | A && B | A || B | | --- | --- | --- | --- | | true | true | true | true | | true | false | false | true | | false | true | false | true | | false | false | false | false |


Assignment Operators

Assignment operators assign values to variables.

Operator Example
= x = 10
+= x += 5
-= x -= 5

Example:

let x = 10;

x += 5;

console.log(x);

Output

15

Another example:

let y = 20;

y -= 5;

console.log(y);

Output

15

These operators help update values easily.


Practical Example

let a = 10;
let b = 5;

console.log(a + b);
console.log(a > b);
console.log(a === 10);

Output

15
true
true

Practice Assignment

1 Perform Arithmetic Operations

Create two numbers and perform:

  • addition

  • subtraction

  • multiplication

  • division

Example:

let a = 8;
let b = 4;

console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);

2 Compare Values

Use both == and ===.

Example:

console.log(5 == "5");
console.log(5 === "5");

Observe the difference.


3 Write a Logical Condition

Example:

let age = 20;
let hasTicket = true;

if (age >= 18 && hasTicket) {
  console.log("You can enter");
}

Explain which logical operator you used and why.