Tech N Toast®

What are JavaScript Operators?

JavaScript Operators

They are used to perform some specific operations on operands. Before we deep dive into them, let us first understand the basic concept.

Symbols like +, -, * etc., which are used for addition +, multiplication *, and subtraction -. We are already aware of these things from our childhood days.

For example, 3 + 4 = 7

3 is left operand

+ is an operator 

4 is right operand


JavaScript talks about various operators:

1. Arithmetic
2. Assignment
3. Comparison
4. Logical
5. Ternary (Conditional)
6. JavaScript String
7. Typeof
8. Bitwise


Talking about everything in detail:



JavaScript Arithmetic Operators

1. + (Addition): + operator performs addition. For example, variable X holds 3 and variable Y holds 4, or you can say that X = 3 and Y = 4. Next, add these two operands (X + Y = 7 or 3 + 4 = 7).

2. – (Subtraction): - operator performs subtraction. Let's perform subtraction on our two operands, X - Y = -1 or 3 - 4 = -1 

3. * (Multiplication): Multiply both operands, X * Y = 12 or 3 * 4 = 12 

4. / (Division): / operator performs division, X/Y = 0.75 or 3/4 = 0.75

5. % (Modulus): % operator gives remainder of an integer division, X % Y = 3 or 3 % 4 = 3 or 3 mod 4 = 3

6. ++ (Increment): ++ operator increases an integer value by one. 

          X = 3 and Y = X++ Result X = 4, Y = 3
          if X = 3 and Y = ++X Result X = 4, Y = 4

7. –– (Decrement): -- operator decreases an integer value by one.

          X = 3 and Y = X-- Result X = 2, Y = 3
          if X = 3 and Y = --X Result X = 2, Y = 2


JavaScript Assignment Operators

1. = (Simple Assignment or Assignment Operator): It assigns the right operand value to the left operand. 

             X = 3 
             Y = X 
             Y = 3

2. += (Add and Assignment): This operator adds left and right operand values. Next, It assigns the result to the left operand. 

              X += 3 
              X = X + 3 

3. –= (Subtract and Assignment): This operator subtracts the right operand from the left operand. Next, it assigns the result to the left operand.

              X -= 3 
              X = X - 3

4. *= (Multiply and Assignment): This operator multiplies left and right operand values. Next, it assigns the result to the left operand.

             X *= Y
             X = X * Y

5. /= (Divide and Assignment): Dividing the left operand value by the right operand value, and assigning the result to the left operand. 

             X /= Y
             X = X / Y

6. %= (Modules and Assignment): Modulus of operands, and assigning to the left operand.

             X %= Y
             X = X % Y

                                                                
Comparison Operators

1. = = (Equal): If both operands are equal, then the condition becomes true. If X = 3 and Y = 4, then (X == Y) is not true. 

2. != (Not Equal): Both operands are equal or not. If the values are not equal, then the condition becomes true. If X = 3 and Y = 4, then    (X != Y) is true. One more example, If X = 3 then X != 4 is true. 

3. > (Greater than): If the left operand greater than the right operand, it returns true.

             X = 3
             X > 4 is false 

4. < (Less then): If the left operand lesser than the right operand, it returns true.

              X = 3
              X < 4 is true

5. >= (Greater than or Equal to): If the left operand greater than or equal to the right operand, it returns true.

             X = 3
             X >= 4 is false 

6. <= (Less than or Equal to): If the left operand lesser than or equal to the right operand, it returns true.

             X = 3
             X <= 4 is true


Logical Operators

1. && (Logical AND): If two operands are non-zero, it returns true, X = 3 and Y = 4, then (X && Y) is true.

2. || (Logical OR): If one of the two operands is non-zero, it returns true, Y = 4 and X = 0, then (Y || X) is true. 

3. ! (Logical NOT): This operator reverses the Boolean result (logical state) of the operand (or condition). True becomes false, and false becomes true, Y = 4 and X = 0, then !(Y || X) is false.



Ternary (Conditional) Operator

It assigns a value to a variable based on a condition.

Syntax:  <condition> ? <value1> : <value2>;

Laptop = (price < 25,000) ? "Affordable":"Expensive";

If the variable "price" is below 25,000, the value of the variable "Laptop" will be "Affordable", otherwise the value of "Laptop" will be "Expensive".



JavaScript String Operator

We can use two operators (+ and +=) to adds two strings (Concatenation). 

text1 = "Technical", text2 = "Books", and text3 = "" 

text3 = text1 + text2

Then, the result of text3 - Technical Books

One more example,

text1 += text2 

Then, the result of text1 - Technical Books



Typeof Operator

It can return values like number, strings, function, Boolean, true or false etc. 

For example, typeof "techNtoast" returns string, typeof false returns Boolean, typeof 7 returns number etc.



Bitwise Operators

It is a 64-bit floating point number in JavaScript. When the bit-wise operation is performed, the number is converted into a 32-bit binary number(signed). Once, the operation is completed, the number is converted back and the result is in 64-bit number.

1. & (Bit-Wise AND): A binary operator returns bit-wise AND operation for two operands.

2. | (Bit-Wise OR): A binary operator returns bit-wise OR operation for two operands. 

3. ^ (Bit-Wise XOR): It returns bit-wise XOR operation for two operands.

4. ~ (Bit-Wise NOT): It returns bitwise NOT operation for a single operand.

5. << (Bit-Wise Shift Left): Left shift of two operands.

6. >> (Bit-Wise Shift Right): Right shift of two operands.

7. Zero fill Shift Right (>>>): Every bit shifts towards right, and the bits on the left are always zero.


Use JavaScript in various devices and applications like web pages, API calls, maps, games, scrolling abilities, Microsoft Dynamics 365 CRM etc.

If you want to test your JavaScript code, you can try here - https://www.techntoast.com/2019/11/test-your-html-javascript-and-css-codes.html




No comments:
Post a Comment