Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

Updated
•3 min read•View as Markdown
JavaScript Operators: The Basics You Need to Know
D

I am a developer learning web development , I am a college dropout pursuing my passion in software field

when we write JS we constantly perform operations on the values, like adding , comparing, making decisions in the condition on the value

Types of operators

In JS we have 4 types of operators

  1. Arithmetic operators : +, -, *, /, %

  2. Comparison operator : ==, ===, !=, <, >

  3. Assignment operator : =, +=, -=

  4. Logical operator : &&, ||, !

Now we will study each of them in depth starting with aritmetic operator

1. Arithmetic operator

So the most basic operator are Arithmetic operator, these operators perform basic mathss calculation
It has 5 operators under it

  • Addition:
    this will simplity add 2 given value

  • Subtraction
    This will simply subtract 2 given value

  • Multiplication:
    this will simply multiply 2 given value

  • Division:
    This will simply divide 2 given value

  • Modulo :
    This will give us the remainder after dividing the 2 value

2.Comparison operator

Comparison operator compare two valus and return a boolean (true or false) value

  • Greater than ( > )
    Check which value is greater that the other

  • Less than ( < )
    Check which value is smaller than the other

  • Abstract equality ( loose comparison ) ( == )
    This operator compares only the values. It can lead to surprising results because JavaScript will first convert the values to a common type before comparing them. like if we compare number 5 with string 5 then it will give true as it dosen't check data type it only check the final value

  • Strict equality ( strict comparison ) ( === )
    This operator compares both the value AND the type. This is the much safer and more predictable option. If the types are different, it's an immediate 'false'

  • Not equal to ( != )
    This operator will check if the given two value are not equal to each other or not, if they are equal then it will give 'false' and if they are not equal then it will return 'true'
    And it only return boolean value

3.Assignment operator

we have already been using the most common assignment operator: the equals sign (=). Its job is straightforward: it assigns the value on the right-hand side to the variable on the left.
There are also shorthand assignment operators that let you perform an operation and update the variable's value in a single, clean step. They are a convenient way to make your code more concise.

  • Assignment operator
    It assigns the value in RHS in the LHS

  • Addition Assignment :
    Adds and assigns the value in RHS in the LHS

  • Subtract Assignment:
    subtract and assigns the value in RHS in the LHS

  • Simmilarly we have multiplication, division, and Modulo Assignment operator

4.Logical operator

Logical operators let you combine two or more Boolean results (true or false) to create complex conditions.

  • Logical AND ( && )
    Returns true only if all of the conditions are true.

  • Logical OR ( || )
    Returns true if at least one of the conditions is true.

  • Logical NOT ( ! )
    Reverses the Boolean value. !true becomes false, and !false becomes true.

Truth Table for Logical Operators

Thank you :)