Good to see you here on "Part 5" of learning JavaScript (Operators). Please I recommend you go back to the previous tutorials on Learning JavaScript in case you have skipped them. What we are learning builds upon each other, else you may not understand the general concept well enough.
Kindly study the following Parts in case you skipped them.
In computer programming, operators are special symbols or keywords that are used to perform various operations on data or variables. In the case of having a task to build software that helps an NGO to know the number of food items that could feed a certain village or community. Such software will rely heavily on operators for its functions.
JavaScript supports a wide range of operators, which are as follows:
1. Arithmetic operators: These are used to perform basic math operations like addition, subtraction, multiplication, division, and modulo (remainder). For example, `+` is the addition operator, `-` is the subtraction operator, `*` is the multiplication operator, `/` is the division operator, and `%` is the modulo operator.
Arithmetic operators: + (addition), - (subtraction), * (multiplication), / (division), % (modulus), ++ (increment), and -- (decrement)
Sample Code - carefully look into the codes and make meaning to them
var x = 5;
var y = 2;
var z = x + y; // addition
console.log(z); // output: 7
z = x - y; // subtraction
console.log(z); // output: 3
z = x * y; // multiplication
console.log(z); // output: 10
z = x / y; // division
console.log(z); // output: 2.5
z = x % y; // modulus
console.log(z); // output: 1
x++; // increment
console.log(x); // output: 6
y--; // decrement
console.log(y); // output: 1
2. Assignment operators: These are used to assign values to variables. For example, `=` is the basic assignment operator that assigns the value on the right to the variable on the left. There are also compound assignment operators like `+=` (addition assignment) and `-=` (subtraction assignment) that combine an arithmetic operation and an assignment operation.
Assignment operators: = (assignment), += (add and assign), -= (subtract and assign), *= (multiply and assign), /= (divide and assign), %= (modulus and assign), <<= (left shift and assign), >>= (right shift and assign), &= (bitwise AND and assign), ^= (bitwise XOR and assign), and |= (bitwise OR and assign)
Sample Code - Carefully look into the codes for understanding
var x = 5;
x += 3; // same as x = x + 3;
console.log(x); // output: 8
x -= 2; // same as x = x - 2;
console.log(x); // output: 6
x *= 2; // same as x = x * 2;
console.log(x); // output: 12
x /= 3; // same as x = x / 3;
console.log(x); // output: 4
x %= 2; // same as x = x % 2;
console.log(x); // output: 0
x <<= 1; // same as x = x << 1;
console.log(x); // output: 0 (binary 00)
x >>= 1; // same as x = x >> 1;
console.log(x); // output: 0
x &= 1; // same as x = x & 1;
console.log(x); // output: 0
x ^= 1; // same as x = x ^ 1;
console.log(x); // output: 1
x |= 2; // same as x = x | 2;
console.log(x); // output: 3
3. Comparison operators: These are used to compare two values and return a boolean value (`true` or `false`). For example, `>` is the greater than operator, `<` is the less than operator, `>=` is the greater than or equal to operator, `<=` is the less than or equal to operator, and `===` is the strict equality operator.
Comparison operators: == (equal to), === (strictly equal to), != (not equal to), !== (strictly not equal to), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to)
Sample code
var x = 5;
var y = "5";
console.log(x == y); // output: true (values are equal)
console.log(x === y); // output: false (types are different)
console.log(x != y); // output: false (values are equal)
console.log(x !== y); // output: true (types are different)
console.log(x > 3); // output: true
console.log(x < 3); // output: false
console.log(x >= 5); // output: true
console.log(x <= 5); // output: true
4. Logical operators: These are used to perform logical operations on boolean values (`true` or `false`). For example, `&&` is the logical AND operator, `||` is the logical OR operator, and `!` is the logical NOT operator.
Logical operators: && (logical AND), || (logical OR), and ! (logical NOT)
Sample Code - Try to understand the code below.
var x = 5;
var y = 2;
console.log(x > 3 && y < 5); // output: true (both conditions are true)
console.log(x > 7 || y < 1); // output: false (both conditions are false)
console.log(!(x > 3)); // output: false (condition is true, but NOT operator reverses it)
5. Bitwise operators: These are used to perform bitwise operations on binary numbers. For example, `&` is the bitwise AND operator, `|` is the bitwise OR operator, `^` is the bitwise XOR operator, and `~` is the bitwise NOT operator.
Bitwise operators: & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (left shift), and >> (right shift)
Sample Code - Try to understand
var x = 5;
var y = 3;
console.log(x & y); // output: 1 (binary AND)
console.log(x | y); // output: 7 (binary OR)
console.log(x ^ y); // output: 6 (binary XOR)
console.log(~x); // output: -6 (binary NOT)
console.log(x << 1); // output: 10 (left shift by 1 bit)
console.log(x >> 1); // output: 2 (right shift by
6. Conditional (ternary) operator: This is a shorthand way of writing an `if-else` statement in a single line. For example, `x > 3 ? "greater than 3" : "less than or equal to 3"` is equivalent to `if (x > 3) { "greater than 3" } else { "less than or equal to 3" }`.
Conditional (ternary) operator: ? : (used for conditional expressions)
Sample Code
var x = 5;
var y = (x > 3) ? "greater than 3" : "less than or equal to 3";
console.log(y); // output: "greater than 3"
7. Comma operator: This is used to separate multiple expressions in a single statement. For example, `var x = 1, y = 2, z = 3;` declares three variables (`x`, `y`, and `z`) and assigns them the values `1`, `2`, and `3`, respectively.
Comma operator: , (used to separate expressions in a statement)
Sample Code
var x = 1, y = 2, z = 3;
console.log(x, y, z); // output: 1 2 3
Conclusion
Wow, that is pretty much a lot of the JavaScript operators we have treated. Kindly note that your ability to create logic in situations relies on a sound understanding of operators in computer programming.
Comments
Post a Comment