Operators In Javascript With Examples

Operators In Javascript With Examples

Arithmetic Operators:-

OperatorSign
Addition+
Subtraction-
Multiplication*
Division/
Modulus (remainder)%
let x = 10 ;
let y = 5;

console.log(x+y)
console.log(x*y)
console.log(x-y)
console.log(x/y)
console.log(x%y)

//Output
15
50
5
2
0

We can also add strings but there are some basic rules lets learn about those rules.

  • if both the values are strings then it will concatenate both the strings for example:

      let nameOne = "Deepak"
      let nameTwo = " Kumar"
    
      console.log(nameOne+nameTwo)
    
      //Output
      Deepak Kumar
    
  • if one of the values is a numeric value like(10,20 etc) then it will convert the numeric one to a string and then it will concatenate both values and the conversion is being done implicitly, similarly, if you want to add the number but the number is in string format then convert the string format number into number by using number function then you can add. and it is applicable for the rest of the operators like (subtraction, and multiplication) for example:

    
      let a = 10
      let b = "566"
      console.log(a+b)
    
      // Output [ here the number 10 is being converted into a string]
      10566
    
      let a = 10
      let b = Number("566")
      console.log(a+b)
    
      Output
      ------
      576
    
      // Subtraction
      let a = 556
      let b = Number("10")
      console.log(a-b)
    
      Output
      ------
      546
    
      // Multiplication
    
      let a = 10
      let b = Number("10")
      console.log(a*b)
    
      Output
      -----
      100
    

Assignment Operator (=):-

The assignment Operator is used to assign value to a variable and its syntax is:

let a = 10;

here I am assigning a value of 10 to a.

if I am writing a = a+10, in this case, the calculation is being done on the right-hand side first which means w already has the value of a which is 10 so now 10+10 so here the value of a is now 20, and now 20 is assigned to a

OperatorDescription
a=bassigning the value of b to a
a+=bassigning the value of a+b to b
a-=bassigning the value of a-b to b
a*=ba= a * b
a/=ba = a / b
a &=ba = a & b

Comparison Operator:-

This Operator is used to do the comparison between two values let's see how many types of operators are there.

OperatorsDescription
>greater than
<less than
> =greater than or equal to
< =less than or equal to
\==equal to
!=not equal to
// Examples for > and <
let age = 16

if (age > 18){
  console.log('This line will execute if age is greater than 18')
} else if (age< 18){
  console.log('This line will execute if age is less than 18')
} 

// example for >= <=

let age = 20

if (age >= 18){
  console.log('this line will execute if age is equal to 20 or greater than 20')
} else if (age<=17){
  console.log('This line will execute if age is less than 18')
} 

// Example for == 
let a = 10
let b = "10"

if (typeof a == typeof b){
  console.log('Both are equal')
}else{
  console.log('Both are not equal')
}
// Example for !=
let a = 10
let b = "10"

if (typeof a != typeof b){
  console.log('Both are not equal')
}else{
  console.log('Both are  equal')
}

Ternary Operator:

The ternary operator is being created using ? and : instead of using if else we can use the ternary operator

anything which is written after the question mark is true and anything which is written after " : " is false let's get into an example.

condition? expressionTrue : expressionFalse

let is_Login = true;
is_Login ? console.log('Logged Out Please'):console.log('Login in Please')

Output
------
Logged Out Please

//Same Example Without Ternary Operator
let is_Login = true;
if (is_Login){
  console.log('Loged Out Please')
}else{
  console.log('Login Please')
}

Difference between == / ===

== (abstract equality comparison operator) and (===) is known as strictly equality comparison operator. Both are used for comparison purposes, the comparison is being done between two values that the value on the left-hand side is equal to the right-hand side or not.

abstract equality comparison operator does not care about its datatype but the strictly equality comparison operator does.

syntax:
a == b 
a === b

And | Or Operator (Logical Operator)

  • And Logical operator is represented by (&&)
xyx && y
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

so from the table, it is very clear and evident that if both conditions are true then only it will give us the result as true otherwise false.

let firstName = "deepak"
let lastName = "Kumar"


if (firstName[0] == "d" && lastName[lastName.length-1] == "r"){
  console.log('Authorization is successfull Login Please')
}else{
  console.log('You are Not Allowed For Login Authorization is unsuccessfull')
}

Or Logical Operator is represented by using ||. it will return true if any one condition is true or else it will return false. if both the condition is false then it will return false

xyx or y
truetruetrue
truefalsetrue
falsetruetrue
falsefalsefalse

let firstName = "deepak"
let lastName = "Kumar"

if (firstName == "deepak" || lastName[lastName.length-1] == "k"){
  console.log('Authorization is successfull Login Please')
}else{
  console.log('You are Not Allowed For Login Authorization is unsuccessfull')
}

Output
-------
console.log('Authorization is successfull Login Please')

//I have purposely written the OR condition wrong because I just wanted to show you that even if the second condition is wrong but still it will give us the output to Login Please because I mentioned earlier that it will check both the conditions and if one condition is true then we will get the result as true, look at the table I have created above for more clarity.