X



Home>Tutorial>

Mastering What is JavaScript Operators and Examples of JavaScript Operators

Nuelson Penuel Sunday, September 26, 2021 Javascript

Summary

In this post, you will learn what is JavaScript operators, types of JavaScript operators.

What is JavaScript operators

JavaScript operators are defined symbols that are used for performing operations on operands. For example: var sum=10+20; Here, + is the arithmetic operator and = is the assignment operator.

What are the Types of JavaScript operators

The following are the types of operators in JavaScript.
  • Arithmetic Operators
  • Comparison (Relational)
  • Operators
  • Bitwise Operators
  • Logical Operators
  • Assignment Operators
  • Special Operators

What is JavaScript Arithmetic Operators

Arithmetic operators as the name imply are used to carry out arithmetic operations on the operands. Examples of these operators are:
  1. +: This symbol is known as "addition".
  2. It is used to add or sum two numbers. A typical example is 320 + 50 = 370 Below is practical use of this "+" operator;
    var a = 1;
    var b = 2;
    var result = parseInt(a) + parseInt(b);
    alert(result);//result = 3;
    
  3. - : This symbol is known as "Subtraction".
  4. It is used to subtract the left number from the right number. A typical example is 3 - 1 = 2. Below is practical use of this "-" operator;
    var a = 1;
    var b = 2;
    var result = parseInt(b) - parseInt(a);
    alert(result);//result = 1;
    
  5. * : This symbol signifies "Multiplication".
  6. It is used to multiply two numbers. A typical example is 1 * 2 = 2 A practical use of this "-" operator;
    var a = 1;
    var b = 2;
    var result = parseInt(a) * parseInt(b);
    alert(result);//result = 2;
    
  7. /: This signifies " Division".
  8. This divide the left number by the right number. A typical example is 100/2= 50 Below is practical use of this "/" operator;
    var a = 4;
    var b = 2;
    var result = parseInt(a) / parseInt(b);
    alert(result);//result = 2;
    
  9. %: This symbol signifies "Modulus" (Remainder).
  10. This returns the remainder of the division. An example is 199%2 = 1
    var a = 199;
    var b = 2;
    var result = parseInt(a)%parseInt(b);
    alert(result);//result = 1;
    
  11. ++ : This signifies "increment".
  12. It is mostly used in loop. A typical example is:
    var i;
    for(for i=0;i<9;i++){
    	alert(i);
    }
    
  13. -- : This signifies "Decrement".
This is also used in loop . A typical example is:
var i = 10;
i--;

What is JavaScript Comparison Operators

In JavaScript, comparison operators are used to compare two values or operands.
  1. == : This means equal to.
  2. It mostly used when you want to compare if one operand is equal to the other. A typical example on how to use this is:
    var a = 1;var b = 1;
    if(a == b){
    	alert("a and b has the same value");
    }
    else{
    alert ("a is not equal to b");
    }
    
  3. === : This represents "Identical" (strict equality sign) .
  4. It is used to compare two values to know if they are of similar types. It is a strict equality comparison operator in JavaScript, which returns false for the values which are not of a similar type. This operator performs type casting for equality. If we compare 2 with "2" using ===, then it will return a false value. A typical example is:
    var a = 1;
    var b = 1;
    var c = "1";
    
    alert (a === 1);//this will return true
    
    alert(a === "1");//this will return false
    
    alert (a === b);//this will return true
    
    alert (a === c);//this will return false;
    
    In the above example, a === b returns true cos they are of the same type i.e number. But a === c returns false cos they are not of the same type even when they share the same value.
  5. != : This signifies Not equal to
  6. This is used to check if one operand is not equal to the other. A typical example is:
    var a = 1;
    var b = 1;
    
    alert (a != 1);//this will return false
    
    alert(a != b);//this will return false
    
  7. !== : Not Identical
  8. This is used to compare if two operands are not identical. A typical example is:
    var a = 1;
    var b = 1;
    var c = "1";
    
    alert (a !== 1);//this will return false
    
    alert(a !== "1");//this will return true
    
    alert (a !== b);//this will return false
    
    alert (a !== c);//this will return true;
    
    In the above example, a !== b returns false cos they are of the same type i.e number. But a !== c returns true cos they are not of the same type even when they share the same value.
  9. > : This means greater than
  10. This, as the name implies is used when there is need to check if the value of one operand is greater than the othero. For example 50 > 5 = true. A typical demonstration is:
    var a = 6;
    var b = 1;
    if(a > b){
    alert("a value is greater than b value");
    }
    
  11. >= : This simply means greater than or equal to.
  12. When you want to check if a value is greater a specific range or exactly the target range. Example is; 54 >= 23 = true A typical demonstration is:
    var a = 6;
    var b = 1;
    if(a >= 6){
    b += 1;
    alert(b);// result is 2
    //We just incremented value of b only when the value of a is greater or equal to 6. Anything less than 6, b value will not increment.
    }
    
  13. < : This denote the Less than symbol e.g 1 < 6 = true. This is used as a negation to greater than symbol. A typical demonstration is:
    var a = 6;
    var b = 1;
    if(b < a){
    b += 1;
    alert(b);// result is 2
    //We just incremented value of b only when the value of b is less than value of a. Anything that makes the value of b to be greater than a, b value will not increment.
    }
    
  14. <= : This represents Less than or equal to i.e 1 <= 2 = true
What less than or equal to sign normally do is to test the left operand against the right operand. If value of left operand is less than right operand value, return true; if left operand value is equal to right operand value, return true.

What is JavaScript Bitwise Operators?

The bitwise operators perform bitwise operations on operands. The bitwise logical operators treat the operands and result as a series of 32 bits (like an integer). AND, OR, and XOR act upon the corresponding bits in the operands and the result. Bit 0 of the first operand combines with bit 0 of the second operand to produce bit 0 of the result; bit 1 of the first operand combines with bit 1 of the second operand to produce bit 1 of the result; and so on. The bitwise operators are as follows:
  1. x & y
  2. This is referred to as Bitwise AND. It returns true (1) where the corresponding bits in x and y are true (1). It returns false (0) where either bit is false (0).
  3. x | y
  4. This represents Bitwise OR. The result is 1 where either of the corresponding bits in x and y is 1, or both are 1. It returns 0 where both bits are 0.
  5. x ^ y
  6. This is known as Bitwise XOR or rather called "exclusive OR". It only returns 1 where either of the corresponding bits in x and y is 1, and the other is 0. Returns 0 where both bits are 0.
  7. ~x
  8. It means Negation. It is used to inverts the bits of x.
  9. x << n
  10. This means left shift. It will shifts the bits in x to the left n positions, discarding bits that fall off on the left, and filling vacated right positions with 0.
  11. x >> n
  12. Right shift with sign preserved. Shifts the bits in x to the right n positions, discarding bits that fall off on the right, and filling vacated left positions with 0, except that the leftmost bit in a x" is preserved.
  13. x >>> n
Right shift. Shifts the bits in x to the right n positions, discarding bits that fall off on the right, and filling vacated left positions with 0

What is JavaScript Logical Operators

JavaScript logical operators are used to compare two or more operands. The following operators are known as JavaScript logical operators.
  1. && Logical AND
  2. || Logical OR
  3. ! Logical Not

What is JavaScript Assignment Operators

The JavaScript assignment operators are used to assign value to an operand. The following operators are known as JavaScript assignment operators. 1. =Assign 10+10 = 20 2. += Add and assi 3. -= Subtract and assign 4. *= Multiply and assign 5. /= Divide and assign 6. %= Modulus and assign

What is JavaScript Special Operators

The following operators are known as JavaScript special operators.
  1. (?:) Conditional Operator
  2. This returns value based on the condition. It is like if-else.
  3. , Comma Operator
  4. This allows multiple expressions to be evaluated as single statement.
  5. delete Operator
  6. This deletes a property from the object.
  7. in Operator
  8. This checks if object has the given property instanceof checks if the object is an instance of given type
  9. new operator
  10. This creates an instance (object) typeof operator This checks the type of object.
  11. void operator
  12. This discards the expression's return value.
  13. yield
  14. This checks what is returned in a generator by the generator's iterator. f checks the type of object. void it discards the expression's return value. yield checks what is returned in a generator by the generator's iterator.

| | Views(309)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services