Learn JavaScript : Operators (Part 5)

JavaScript Operator


"Operators are the starting point in creating a suitable logic for a situation, make sure to understand them very well." (Mark Anthony Graham)


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.

What is an operand?

In computer programming, an operand is a value or variable on which an operator operates. In other words, an operand is a data element that is manipulated by an operator. 

For example, in the expression 2 + 3, the operands are 2 and 3, and the operator is the addition (+) operator. The expression can be read as "add 2 and 3" or "perform the addition operation on 2 and 3." The result of the operation is 5.

In another example, consider the expression x * y, where x and y are variables. In this expression, the operands are the variables x and y, and the operator is the multiplication (*) operator. The expression can be read as "multiply the value of x and y" or "perform the multiplication operation on the values stored in x and y."

It's important to note that not all operators require two operands. Some operators, like the unary minus (-) operator, only require one operand. Additionally, some operators may have different numbers of operands, like the ternary conditional operator (?:) in some programming languages, which takes three operands.

Understanding expressions

In computer programming, an expression is a combination of one or more values, variables, operators, and function calls that can be evaluated to produce a single value. 

Expressions can be used in many different contexts in programming, such as:
  1.  Assigning a value to a variable: `var x = 5 + 3;`
  2.  Using a value as an argument in a function call: `console.log(x * 2);`
  3.  Comparing values using comparison operators: `if (x > y) { ... }`

Expressions can be simple or complex, and can involve any number of values and operators. Some examples of expressions in JavaScript include:
  1.  `5 + 3` (addition expression, evaluates to 8)
  2. `x = 2 * y - 1` (assignment expression, assigns the value of `2 * y - 1` to `x`)
  3. `x === y || z < 10` (logical expression, evaluates to `true` or `false` depending on the values of `x`, `y`, and `z`)
  4. `myFunction(x + y, z * 2)` (function call expression, calls the `myFunction` function with two arguments: `x + y` and `z * 2`). Do not worry if you do not yet understand functions we will learn about them later on in the tutorial.

JavaScript Operators 

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.

Proceed to Part 6 : JavaScript Else If statements

Comments

followers

Popular posts from this blog

The Beauty Of Using Flutter With Dart For Mobile Development

Building a Fortified Database Connection in PHP

Mastering Error Handling in PHP: A Deep Dive into `@` and `try...catch`