In our last study, we listed the different types of operators. But since you will be using the arithmetic, comparison, and assignment operators more, we will be using coding examples to explain them better in this article.

The arithmetic and assignment operators are important tools in different programming languages. The comparison operators tell the computer what to do and what not to do. You need them in almost every aspect of programming with JavaScript.

Let’s start with the arithmetic operator.

Arithmetic Operators: Code Examples

The arithmetic operators we will be using for this exercise include:

+

*

**
%
++
// the code below intitializes a,b,c
var a, b, c;

// assign values
a = 23;
b = 4;

// + addition operation
c = a + b
console.log(c)

// - minus operation
c = a - b
console.log(c)

// * multiplication operation
c = a * b
console.log(c)

// / division operation
c = a / b
console.log(c)

// % mod operation
c = a % b
console.log(c)

//let's use a new variable
aa = 5;


// the code below will increment the value of aa by 1
aa++

console.log(aa)

// since 1 was added to aa  to become 6, our new aa value is now 6
// -- means 1 will be removed from a
aa--
console.log(aa)

You can also try mixing these operators up to solve complex problems.

// the code initializes the variable
var a, b, d, e, c;

// assign values to the variables
a = 23;
b = 4
d = 5
e = 10

// this expression divides the multiple of a and b by d
c = a * b / d

console.log(c)


// this expression subtracts a from e then raise the answer to power of b
c = a - e ** b

console.log(c)

Comparison Operators: Code Examples

The comparison operator returns a boolean (true or false) output. This is why it is commonly used in an if-else statement or loops. The comparison operators we will be using for this exercise include;

==
===
! =
! ==
<
>
>=
<=
// the code initializes the variable
var xxa, yyb, zz, e, c;


// assign values to the variables
xxa = 23;
yyb = 6;
zz = 5;
e = 10;

// this code checks if xxa is equal to yyb
if( xxa == yyb){

console.log(xxa, "is equal to ", yyb);

}

else{

console.log(xxa, "is not equal to ", yyb);

};



// this code checks if xxa values and typeof is same as yyb
if( xxa === yyb){

    console.log(xxa, "is fully equal to ", yyb);
   
    }
   
    else{
   
    console.log(xxa, "is fully not equal to ", yyb);
   
    }

// this code checks if xxa is not equal to yyb
if( xxa != yyb){

    console.log(xxa, "is not equal to ", yyb);
   
    }
   
    else{
   
    console.log(xxa, "is not equal to ", yyb);
   
    };

// this code checks if xxa values and typeof is not same as yyb
if( xxa !== yyb){

    console.log(xxa, "is fully equal to ", yyb);
   
    }
   
    else{
   
    console.log(xxa, "is fully not equal to ", yyb);
   
    };

// this code checks if xxa values is greater than yyb
if( xxa > yyb){

    console.log(xxa, " > ", yyb);
   
    }
   
    else{
   
    console.log(xxa, "is not >", yyb);
   
    };

   
// this code checks if xxa values is less than yyb
    if( xxa < yyb){

        console.log(xxa, "is less than ", yyb);
       
        }
       
        else{
       
        console.log(xxa, "is not less than ", yyb);
       
        };
   

       
// this code checks if xxa values is greater or equals to yyb
if( xxa >= yyb){

    console.log(xxa, "is > or =to ", yyb);
   
    }
   
    else{
   
    console.log(xxa, "is not > or = to ", yyb);
   
    };

// this code checks if xxa values is less or equals to yyb
    if( xxa <= yyb){

        console.log(xxa, "is < or = to ", yyb);
       
        }
       
        else{
       
        console.log(xxa, "is not < or = to ", yyb);
       
        }

Assignment Operators: Code Examples

The assignment operators we will be using for this exercise are:

=
+=
-=
*=
/=
%=
**=

It is essential that you comprehend each of these operators’ actual meanings before we start writing sample codes using them. For example, you already know that = is for assigning values to variables, so what of += and the rest?

x += y means x = x + y
x – = y meansx = x – y
x *= y means x = x * y
x /= y means x = x / y
x %= y means x = x % y
x **= y meansx = x ** y
// the code initializes the variable



var a, b, d, e, c;


// assign values to the variables
a = 23;
b = 6
d = 5
e = 10



// adds value of a to b
a += b

console.log(a)


// subtracts value of a from b
a -= b
console.log(a)


// multiplies value of a to b
a *= b
console.log(a)


// divides value of a to b
a /= b
console.log(a)

a **= b
console.log(a)


// checks for the mod(remainder) of  a after it divides b
a %= b
console.log(a)

// this code checks if what is left of a after the series of operation is greater than b 
if( a > b){
   

console.log(a, "is equal to ", b)

}

else{

console.log(a, "is not equal to ", b)

}

Note: Don’t worry if you don’t understand how statements are used in these examples. Simply pay close attention to how operators operate, then play around with the sample codes until you fully comprehend how they operate. You will learn about the if-else statement and how it functions in a custom article that is soon to be published. 

Conclusion

In this article, we used some code examples to explain how you can use different assignment, arithmetic, and comparison operators in JavaScript. You can go ahead and practice with the other operators. In our next article, we will look at functions.

Categorized in: