Using Operators to coding in Javascript/JS

This JavaScript tutorial explores the a number operators reachable in the JavaScript language with syntax and examples.

Description

Operators are used in JavaScript code to perform comparisons, mathematical operations, and assignments. Let’s take a seem to be at the one of a kind types of operators.

Comparison Operators

Comparison operators are the operators that you would use to examine for equality, inequality as well as price (or data type) differences. Below is a listing of the comparison operators in JavaScript:

Operator Data Type Description Example == any Tests for equality (performs implicit information kind conversion) h == 15 != any Tests for inequality (performs implicit data kind conversion) h != 7 === any Tests for equality and same facts kind h === 3 !== any Tests for equality and same records type h !== 4 > numbers, strings Greater Than h > 12 >= numbers, strings Greater Than or Equal h >= 9 < numbers, strings Less Than h < 5 <= numbers, strings Less Than or Equal h <= a hundred

Mathematical Operators

Mathematical operators are the operators that you would use to operate math operations such as addition, subtraction, multiplication and division. Below is a listing of the mathematical operators in JavaScript:

Operator Data Type Description Example + numbers Addition h = h + 8 – numbers Subtraction h = h – 4 * numbers Multiplication h = h * 12 / numbers Division h = h / 10 % numbers Remainder after division is carried out h = h percent 7 ++ numbers Increment (pre-increment or post-increment) ++h h++ — numbers Decrement (pre-decrement or post-decrement) –h h–

Assignment Operators

Assignment operators are the operators that you would use to assign a cost to a variable. Below is a list of the venture operators in JavaScript:

Operator Data Type Description Example = any variable Assign fee h = 8 += any variable Add and assign value (same as h = h + 3) h += 3 -= any variable Subtract and assign value (same as h = h – 12) h -= 12 *= any variable Multiply and assign value (same as h = h * 7) h *= 7 /= any variable Divide and assign value (same as h = h / 5) h /= 5 %= any variable Divide and assign remainder value (same as h = h percent 10) h %= 10 <<= any variable Left shift and assign value (same as h = h << 3) h <<= 3 >>= any variable Right shift with signal extension and assign value (same as h = h >> 9) h >>= 9 >>>== any variable Right shift with zero extension and assign value (same as h = h >>> 17) h >>>= 17 &= any variable Bitwise AND and assign value (same as h = h & 6) h &= 6 ^= any variable Bitwise XOR and assign value (same as h = h ^ 4) h ^= 4 |= any variable Bitwise OR and assign value (same as h = h | 50) h |= 50

Logical Operators

Below is a list of the logical operators in JavaScript:

Operator Data Type Description Example && boolean Logical AND (h == forty && j > 2) || boolean Logical OR (h == 35 || j < 10) ! boolean Logical NOT (inverts the boolean value) !(h <= 7)

String Operators

Below is a listing of the string operators in JavaScript:

Operator Data Type Description Example + string Concatenate h = “Tech” + “OnTheNet” The variable h would include the fee “TechOnTheNet” += string Concatenate by means of appending to the give up of the string h += “OnTheNet” If the variable h starts with a cost of “Tech”, the += operator would append “OnTheNet” to “Tech” and the variable h would include the price “TechOnTheNet”

Bitwise Operators

Below is a list of the bitwise operators in JavaScript:

Operator Data Type Description Example & integer (32 bit number) Bitwise AND h = h & 6 ^ integer (32 bit number) Bitwise XOR h = h ^ 4 | integer (32 bit number) Bitwise OR h = h | 50 << integer (32 bit number) Left shift h = h << 7 >> integer (32 bit number) Right shift with signal extension h = h >> 3 >>> integer (32 bit number) Right shift with zero extension h = h >>> 22

Conditional Operator

The conditional operator (known as the ?: operator) is a unique operator that approves you to assign one fee when the circumstance is genuine and some other price when the condition is false. This operator requires a little more explanation, so let’s get started!

The syntax for this conditional operator is:

variable_name = (condition) ? true_value : false_value;

Parameters or Arguments

variable_name The variable that will be assigned a value. condition The situation to be evaluated. true_value The price that is assigned to variable_name when the situation is true. false_value The fee that is assigned to variable_name when the condition is false.

Example

Here is an instance of how to use this conditional operator:

numType = (x < 0) ? "Negative" : "Positive";

In this example, if x is less than 0, the cost “Negative” will be assigned to the variable numType. Otherwise, the fee “Positive” will be assigned to numType.