JavaScript Basics: Operators

JavaScript Basics: Operators

Hello Everyone, Greetings of the Day :). I hope you are fine and must be learning JavaScript. In the previous 2 articles, I have explained you about Basics of JavaScript and Variables. So before moving to this article, I request you to please read them all.

Let's start learning JavaScript Operators.We'll see it one by one.

What is Operators ?

In JavaScript, there are mainly 4 Types of Operators :

  • Assignment Operators

  • Arithmetic Operators

  • Comparison Operators

  • Logical Operators

What is Assignment Operators ?

In our previous article, we had seen about the Variables. If you notice its syntax for ex :

 <script>

       var a = 10 ;     // assign the value 10 to a
       var b = 20 ;     // assign the value 20 to b
       var c = a+b;     // assign the sum of a and b to c     

    </script>

The assignment operator (=) assigns a value to a variable. Value could be anything like Number, String, Array, Object or even a variable too etc.

Let's understand more about assignment operator by looking into this table.There are several types of assignment operator available in JavaScript. If you want to learn more about this, please visit this link

JavaScript Assignment Operators.png

Arithmetic Operators :

As it name indicates, Arithmetic, it is used to perform mathematical operations. " +, -, , /, %* " are example of Arithmetic Operators

 <script>

        var a = 3;
        var x = (100 + 50) * a; // Output 450

    </script>

According to BODMAS Rule, first it will add 100+50 which is inside the (), the result would be 150, then multiply with a which is 3. Hence the output would be 450. In the same way,we can perform other mathematical calculations too.

Comparison Operators :

Again,its name tells everything about what this operators does. Yes, you are correct ! . It is used for comparing value of variables or conditions. Mostly , it is used in logical statements to determine equality or difference.

Let's understand it with a simple example. In most of the countries, if you are under 18, you can't drive.

Similarly, here we are using comparison operators to check whether user age is above 17 or not. Above 17 means 18,19,20....and so on. Don't worry about Conditional Statements, we'll clear it in our upcoming article.

   <script>

            var userAge = 17;

            if (userAge > 17 ) 
            {
                console.log("You can drive");    
            }
            else
            {
                console.log("Sorry ! You can't drive");
            }

             //    Output : Sorry ! You can't drive

    </script>

The above output was Sorry ! You can't drive. But why ? Let me explain you. In the first if{} block, we are checking that if user's age is greater than 17, that is 18,19,20....then he/she will be eligible for driving. So we have already set a value to userName variable 17, so this value 17, doesn't fit into if{} block statements, thats why we have moved to next block of code i.e. else part.

Therefore, condition is not satisfied in if block, thats why it is printed "Sorry ! You can't drive"

Logical Operators :

Logical operators are also used to check the logic between variables and values. These are AND (&&), OR (||) and NOT (!).

For example, a contest is schedule where age of student only should be 20, not less than 20 or greater than 20. So we'll use Logical operators && :

  <script>

            var studentAge = 20;

            if (studentAge < 20 && studentAge > 20)
             {
                console.log("You are eligible");
            }

            else
            {
                console.log("You are Not eligible");  // Output : You are Not eligible"  
            }


    </script>

Here How the other Operators works :

logical operators.jpeg

Hope, this above article on Operators clear your doubts. By reading or watching article or any youtube video just a once or twice won't help you. Along with reading or watching, you must have to practice codes regularly to stand out of the crowd.