Skip to main content

JavaScript Operators (Arithmetic, Assignment, Comparison, Logical Conditional Ternary, Bitwise)

 

JavaScript Operators (Arithmetic, Assignment, Comparison, Logical Conditional Ternary, Bitwise)



JavaScript, operators are symbols or keywords used to perform operations on one or more values. JavaScript has several types of operators, which includes the following below.

Arithmetic Operators





Arithmetic Operators: These operators are used for basic arithmetic operations such as addition, subtraction, multiplication, and division. The addition operator + can also be used for string concatenation. Examples of arithmetic operators include +, -, *, /.

Addition (+)



 


In this example above, I have used the addition operator is used to add two or more numbers or to concatenate two or more strings.

Subtraction (-)




In the example above, I have used the subtraction operator is used to subtract one number from another.

 

Multiplication (*)



 

In the example above, I used the multiplication operator is used to multiply two or more numbers.

 

Division (/)

 


 


In the example above, I have used the division operator is used to divide one number by another.

 

 

Remainder/Modulus (%)

 


 

In this example above, I have used the remainder/modulus operator is used to find the remainder of dividing one number by another.

 

Increment (++)

 


 

In this example above, I have used the increment operator is used to increase the value of a variable by 1.

Decrement (--)

 


 


.In this example above, I have used the decrement operator is used to decrease the value of a variable by 1.

Exponentiation (**)

 


 

In this example above, I have used the exponentiation operator is used to raise a number to a power.

Assignment Operators









Assignment Operators: These operators are used to assign a value to a variable. For example, the assignment operator = assigns the value on the right to the variable on the left. Other examples of assignment operators include +=, -=, *=, /=.

The addition assignment operator (+=)

 


 

 

In this example above, I have used the addition assignment operator to add the value on the right-hand side to the variable on the left-hand side and assign the result to the variable on the left-hand side.

 

The subtraction assignment operator (-=)

 

 


 


In this example above, I have used the subtraction assignment operator to subtract the value on the right-hand side from the variable on the left-hand side and assign the result to the variable on the left-hand side.

 

The multiplication assignment operator (*=)

 

 


 

In this example above, I have used the multiplication assignment operator to multiply the variable on the left-hand side by the value on the right-hand side and assign the result to the variable on the left-hand side.

 

The division assignment operator (/=)

 


 


In this example above, I have used the division assignment operator to divide the variable on the left-hand side by the value on the right-hand side and assign the result to the variable on the left-hand side.

 

The modulus assignment operator (%=)

 


 

In this example above, I have used the modulus assignment operator to calculate the remainder of the division of the variable on the left-hand side by the value on the right-hand side and assign the result to the variable on the left-hand side.

 

The exponentiation assignment operator (**=)

 


 

In this example above, I have used the exponentiation assignment operator to raise the variable on the left-hand side to the power of the value on the right-hand side and assign the result to the variable on the left-hand side. In this example, 2 raised to the power of 3 is 8.

Comparison Operators






Comparison Operators: These operators are used to compare two values and return a Boolean value (true or false). Examples of comparison operators include ==, !=, ===, !==, <, >, <=, >=.


The equality operator (==)



 

The equality operator compares the values of two operands and returns true if they are equal, regardless of their data types.

 

 

The strict equality operator (===)

 



The strict equality operator compares the values and the data types of two operands and returns true if they are equal. In this example, x is a number, and y is a string, so they are not strictly equal.

 

The not equal operator (!=)

 



The not-equal operator compares the values of two operands and returns true if they are not equal.

 

The strict, not equal operator (!==)

 




The strict, not equal operator compares the values and the data types of two operands and returns true if they are not equal.

 

The greater than operator (>)

 




The greater than operator compares two operands and returns true if the left operand is greater than the right operand.

 

The less-than operator (<)

 




The less-than operator compares two operands and returns true if the left operand is less than the right operand.

 

The greater than or equal to the operator (>=)

 




The greater than or equal to operator compares two operands and returns true if the left operand is greater than or equal to the right operand.

 

The less than or equal to the operator (<=)

 




The less than or equal to operator compares two operands and returns true if the left operand is less than or equal to the right operand.

Logical Operators






Logical operators: These operators are used to perform logical operations on Boolean values. Examples of logical operators include && (AND), || (OR), ! (NOT).

Logical AND (&&)

 




In this example above, I have used the logical AND operator (&&) to check if both a is greater than 4 and b is greater than 2. Since both conditions are true, the value of c is true.

 

Logical OR (||)

 




In this example above, I have used the logical OR operator (||) to check if either a is greater than 6 or b is less than 2. Since neither condition is true, the value of c is false.

Logical NOT (!)

 




In this example above, I have used the logical NOT operator (!) to check if a is not greater than 4. Since a is indeed greater than 4, the value of b is false.

Combining logical operators

 




In this example above, I have used a combination of logical operators to check if a is not greater than 4 OR b is less than 2. Since the second condition is true, the value of c is true. Note that the logical NOT operator (!) has higher precedence than the logical OR operator (||). Therefore, the expression !(a > 4) is evaluated first before it is combined with the second condition using the logical OR operator.

Bitwise Operators





Bitwise Operators: These operators are used to perform bitwise operations on binary representations of numbers. Examples of bitwise operators include & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), >> (right shift), >>> (zero-fill right shift).

Bitwise AND (&) Operator





In this example above, I have used the bitwise AND (&) operator to perform a bitwise AND operation between the binary representations of the numbers 5 and 3. The binary representation of 5 is 0101, and the binary representation of 3 is 0011. Performing a bitwise AND between these two numbers results in the binary number 0001, which is equal to 1 in decimal notation.

Bitwise OR (|) Operator



 



In this example above, I have used the bitwise OR (|) operator to perform a bitwise OR operation between the binary representations of the numbers 5 and 3. The binary representation of 5 is 0101, and the binary representation of 3 is 0011. Performing a bitwise OR between these two numbers results in the binary number 0111, which is equal to 7 in decimal notation.

Bitwise XOR (^) Operator





In this example above, I have used the bitwise XOR (^) operator to perform a bitwise XOR operation between the binary representations of the numbers 5 and 3. The binary representation of 5 is 0101, and the binary representation of 3 is 0011. Performing a bitwise XOR between these two numbers results in the binary number 0110, which is equal to 6 in decimal notation.

 

Bitwise NOT (~) Operator



 


In this example above, I have used the bitwise NOT (~) operator to perform a bitwise NOT operation on the binary representation of the number 5. The binary representation of 5 is 0101. Performing a bitwise NOT operation on this number results in the binary number 1010 in 2's complement notation. In decimal notation, this is equal to -6.

Bitwise Left Shift (<<) Operator

 





In this example above, I  have used the bitwise left shift (<<) operator to perform a bitwise left shift operation on the binary representation of the number 5. Shifting the binary representation of 5 two bits to the left results in the binary number 010100, which is equal to 20 in decimal notation.

Bitwise Right Shift (>>) Operator

 





In this example above, I have used the bitwise right shift (>>).

Bitwise Zero-Fill Right Shift






In this example above, I have used the bitwise zero-fill right shift (>>>) operator to perform a bitwise zero-fill right shift operation on the binary representation of the number -10. The binary representation of -10 in 2's complement notation is 11111111111111111111111111110110. Shifting this binary number two bits to the right with zero-fill results in the binary number 00111111111111111111111111111101. In decimal notation, this is equal to 1073741821.

Conditional (Ternary) Operator

Conditional (ternary) operator: This operator is a shorthand way of writing an if-else statement in one line. The syntax is condition? expression1 : expression2. If the condition is true, expression1 is evaluated, otherwise, expression2 is evaluated.
conditional operator to set the value of the message variable based on the value of the age variable. by Ziggy rafiq



In the example above, I have used the conditional operator to set the value of the message variable based on the value of the age variable. The syntax of the conditional operator is condition ? expression1: expression2, where the condition is evaluated as either true or false, and if it's true, expression1 is returned, otherwise expression2 is returned.

In this example above, the condition (age >= 18) is evaluated as true since the age is 20 which is greater than or equal to 18. Therefore, the expression "You are an adult" is returned and assigned to the message variable. If the age was less than 18, the condition would have been evaluated as false, and the expression "You are minor" would have been assigned to the message variable instead.

Overall, the conditional operator is a shorthand way of writing an if-else statement in one line and can be useful for concise conditional expressions.

Comma Operator

Comma Operator: This operator is used to evaluate multiple expressions, returning the result of the last expression. The syntax is expression1, expression2, ... expression.

comma operator to evaluate three expressions within parentheses: x += 1, y += 2, and z += 3 by Ziggy Rafiq


In the example above, I have used the comma operator to evaluate three expressions within parentheses: x += 1, y += 2, and z += 3. These expressions modify the values of the variables x, y, and z. The comma operator evaluates each expression from left to right and returns the result of the last expression, which in this case is z += 3. The value of the result is therefore 6, which is the result of the last expression, and the values of x, y, and z are 2, 4, and 6, respectively, after the three expressions are evaluated.

Comments

Most Viewed Ziggy Rafiq Blog Posts

A Complete Guide to Using GUIDs in C# with Code Examples

  Overview In this post, we are looking at GUIDs (Globally Unique Identifiers), which are widely used in C# for generating unique identifiers for objects, entities, and resources in a system. In this post, we'll explore the basics of GUIDs in C#, their advantages, and how to convert strings to GUIDs. In this post, we have used Guid Generator to create the GUID from the following URL Address https://guidgenerator.com/ What is GUID GUID (Globally Unique Identifier) in C# is a 128-bit value that is used to identify objects, entities, or resources in a unique manner across different systems and applications. It is also known as UUID (Universally Unique Identifier) in some other programming languages.   GUIDs are generated using a combination of unique factors such as the MAC address of the network adapter, the current time and date, and a random number. The resulting GUID is a string of 32 hexadecimal digits separated by hyphens, such as "b86f2096-237a-4059-8329-1bbcea72769b...

Primitives Data Types and None-Primitives Data Types in C# with Code Examples

  Overview I wrote this post to provide an explanation of primitive and non-primitive data types in C#. C# is a strongly typed programming language, where each variable and expression must have a specific data type. C# data types are categorized into two primary groups: primitive data types and non-primitive data types. Primitive data types are the simplest data types available in programming languages. They are typically pre-defined data types and can represent a single value, such as a boolean value, character, or integer. Examples of primitive data types include int, char, float, double, and boolean, which are common in programming languages like C++, C, and Java. Non-primitive data types are also referred to as composite data types or reference data types. They are constructed from primitive data types and are more complex than primitive data types. Non-primitive data types can hold multiple values and allow for the creation of more intricate data structures like tables, li...

How to use Enum Data Values with .Net 6.0 Framework and Entity Framework Core 6

How to use Enum Data Values with .Net 6.0 Framework and Entity Framework Core 6 Overview An Enum (Enumeration) is a group of constants that are read-only value types. By default, the first value of the Enum variable is 0 i.e. Here we will create an Enum of Priorities type with read-only values of Highest, Normal and Low. We will set the read-only values using an integer assigning a number next to the value. By default, the integer value will start with 0. Here we will be assigning the integer value next to the Enum value such as in the below example and we will use a comma (,) to separate the item in the list of Enum(Enumeration).  We create Enum by using the Enum keyword and then using class, interface, and abstract. The reason we use an Enum is to ensure we improve our application performance and improve application readability, and maintainability, and reduces the complexity of the application hence why if you take a look at the example below of Status (NotStarted, Started, Comp...