Operators in Javascript (Basic Operators)

  • Post category:Javascript
  • Reading time:3 mins read

In this post, I will discuss basic operators in javascript that will be used mostly in daily coding assignments or projects.

Operators in Javascript

An operator basically allows us to transform values or combine multiple values. There are many types of operators such as mathematical operators, comparison operators, logical operators, assignment operators, and many others.

So, let’s start learning more about operators in javascript using some examples.

Addition and Subtraction:

let randomSum = 1000 + 500;
let subtract = 1000 - 500;
console.log(randomSum, subtract);
Output: 1500, 500

In the above code, I did addition and subtraction using + and –

Mathematical Operators in Javascript

Add, Subtract, Divide, Multiply, Power, etc

let num = 22;
console.log(num + 2, num - 2, num * 2, num / 2, num % 2, num ** 2, num ** 3);
Output: 24, 20, 44, 11, 0, 484, 10648

Let’s understand the above code.

  1. num + 2 means 22 plus 2. So, the output will be 24.
  2. num – 2 means 22 subtracted by 2, So, the output will be 20.
  3. num * 2 means 22 multiplied by 2, as we have stored 22 in num.
  4. num/2 means 22 divided by 2.
  5. num%2 means the quotient that we will get after dividing 22 by 2. So, in this case, we get 0 remainder after dividing 22 by 2.
  6. num**2 means the square of num. So if we do the square of num then we will get 484.
  7. num**3 means the cube of num.

Note: You can put any power to a value. You just need to use ** and write the value. Suppose I want to calculate 2 to the power 5 (25). I can write it as 2**5.

Adding two strings using +

const city = "Delhi";
const country = "India";
console.log(city + " " + country);
Output: Delhi India

Explanation:

  1. I have used  + to concatenate two strings. So, if I want to add 2 or more strings together then I can use the + operator.
  2. In the above code, I have added ” “ to provide the gap between the two strings.

Assignment Operators in Javascript

let p = 100;
p += 5; // p = p + 5;
p *= 2; // p = p * 2;
p++; // p = p + 1;
p--; // p = p - 1;
p++;
console.log(p);
Output: 211

let’s consider some examples to add numbers in Javascript.

let num = 120 + 20;
num += 60;  // it means num = num + 60;
console.log(num);
Output: 200

In the above code, num will store the value 140 and then in the next line num will get updated with 60 plus. So, it will give an output of 200.

let num = 40;
num *= 6; // it means num = num * 6;
num++; // it means num = num + 1;
console.log(num);
Output: 241

Let’s understand this code line by line.

  1. I have assigned 40 to num variable
  2. I have multiplied num by 6. So, the value becomes 240.
  3. Now, I did num++ which means increasing the num by 1. I have added 1 to the value. So, it becomes 241.

Comparison Operators in Javascript

comparison operators:
>
<
==
>=
<=

Example:

let x = 42;
let y = 20;
console.log(x > y);
Output: true

In this code, I have compared the x and y variables using the comparison operator. x is greater than y. So, it will give the output as true.

Share this