Skip to main content

Command Palette

Search for a command to run...

JavaScript Operators: The Basics You Need to Know

Published
5 min read
JavaScript Operators: The Basics You Need to Know
S
I write code , that run in the browser and someone else's machine. And sometimes I also write articles

In previous blog in our JavaScript Introduction Series we learnt about variable declaration and basics of data types in JavaScript. There we learnt that we can store various values of datatypes like String and Number inside a variable.

In this blog we will learn how to perform operations on those values inside JavaScript. To do that JavaScript provides us some common operations that we can do on values directly or while stored inside variables and transform them as we see fit.

Need of Operators

Suppose you want to create a program that adds two numbers ... How are you going to do that? How do you normally add two numbers? using + or addition operation correct? Similarly, we can perform additions and other mathematical operation like 5 + 10 = 15 inside our JavaScript program itself.

In JavaScript not only just mathematical but also other kinds of operations can be done too. Let's learn about them in orderly manner.

Arithmetic Operators

Arithmetic or mathematical operation is very common type of operation we can perform on values or variables inside JavaScript.

First we have,

Addition operation +

As you can see it is very simple to perform an operation in JavaScript, you need to provide the appropriate operator (the symbol of operation to be performed, here it is + of addition operator) and the operands or the values on which the operation to be performed, here 5 and 10 are our operands.

console.log() is a function that simply prints the output of the value provided which in this case is output of the + operation.

Subtraction operation -

For subtraction we use - operator.

Multiplication operator *

Multiplication operator is denoted using * symbol.

Division operator /

Division operator is denoted as a forward slash symbol.

Remainder operator %

To find remainder of a division operator we use this operator

Comparison operators

Sometimes you need to compare two values and judge it's output for that we use comparison operators. These operators return either true or false as values. These values are known as boolean datatype.

Equal operator ==

This operator returns true if the both operands are of same value.

Equal operator does not care about the data type of the the operands involved, as long as their magnitude or values are identical.

Not-equal operator

This operator returns true if both operands are different

Strict-equal operator

This operator uses 3 = like === to equate two values and this operator cares about the datatype of the operands involved, it returns true only and only if both the value and datatype of both operands are same.

same like non strict not-equal strict not-equal operator also exist and it is datatype sensitive like strict equal. It is denoted using !==

Greater than operator >

This operator checks if left operand is bigger than the right operand.

Also if you want you can check if a value is either same or greater than a another value using greater than equal operator >=

Less than operator <

This operator checks if left operand is smaller than the right operand.

also like greater than equal operator there is also less than equal operator <=

Logical operators

Logical operators only work on boolean data types of values (e.g. true or false ) , the operands can be simple true or false value or another expression that results in true or false , like output of comparison operators.

AND operator &&

This operator can work on two or more operands at the same time, this operator results in true if and only if all the operands are true

Or operator ||

Or operator can also take more than two operands and if even a single operand is true the result of the whole expression is true, if and only if all the operands are false the expression results in a false value.

Logical NOT operator !

This operator takes only one operand and flips it to the opposite value (e.g. true becomes false and vice versa)

Assignment operators

Assignment operators work as short-hand for combining assignment operators with arithmetic operators. Most basic assignment operator is = which is called assignment operator .. it is used to assign a value to a variable or value of one variable to another.

Addition assignment +=

This is same as combining a = a + b;
Remember the left operand has to be the same variable where the result of the whole expression will be stored, here in our case it is a variable.

Subtraction assignment -=

This is same as addition assignment but for subtraction operation.

In same way the multiplication assignment *= , Division assignment /= and Remainder assignment %= work.

Conclusion

These are the most commonly used operation in most cases in JavaScript, if you grasp the working of all the above mentioned operators you should have no issues in your day to day JavaScript programming.