Chapter-2:
“Yesterday, Easy Coding School explored Arithmetic Operators, Assignment Operators, and Comparison Operators. We still have pending discussions on other types of operators.”
Logical Operators:
Logical operators in JavaScript are used to combine or modify Boolean values (true or false). They allow you to perform logical operations and make decisions based on the results of these operations. JavaScript provides three main logical operators: AND (logical &&), OR (logical ||), and NOT (logical !). Here’s an explanation of each with examples:
Also read more, Chapter -1: Javascript Operators and Expressions
Logical AND (&&): The logical AND operator returns true if both operands are true, otherwise it returns false.
let isAdult = true;
let hasLicense = true;
let canDrive = isAdult && hasLicense; // true (both conditions are true)
Logical OR (||): The logical OR operator returns true if at least one of the operands is true, otherwise it returns false.
let hasPermission = false;
let isAdmin = true;
let canAccess = hasPermission || isAdmin; // true (at least one condition is true)
Logical NOT (!): The logical NOT operator negates the value of its operand. It returns true if the operand is false, and false if the operand is true.
let isLoggedOut = true;
let isLoggedIn = !isLoggedOut; // false (negating the true value)
Logical operators are often used to create more complex conditions by combining simpler conditions. They are particularly useful in conditional statements (if, else if, else) and loops to control the flow of your program based on various conditions. For example:
if (isAdult && hasLicense) {
console.log("You can drive!");
} else {
console.log("You cannot drive.");
}
Ternary (Conditional) Operator:
The ternary operator, also known as the conditional operator, is a concise way to write conditional expressions in JavaScript. It’s often used to make decisions and assign values based on a condition. The ternary operator takes the form condition ? expressionIfTrue : expressionIfFalse. Here’s an explanation with an example:
let age = 18;
let status = (age >= 18) ? 'Adult' : 'Minor';
In this example, the ternary operator checks if age is greater than or equal to 18. If the condition is true, the value ‘Adult’ is assigned to the status variable. If the condition is false, the value ‘Minor’ is assigned.
The ternary operator is a shorthand way to write a simple if…else statement. The code above is equivalent to the following:
let age = 18;
let status;
if (age >= 18) {
status = 'Adult';
} else {
status = 'Minor';
}
The ternary operator is especially useful when you want to make a quick decision between two values and assign one of them based on a condition. However, for more complex conditions or scenarios that involve multiple branches, using a regular if…else statement might be more readable.
Bitwise Operators:
Bitwise operators in JavaScript are used to perform operations on the individual bits of binary representations of integers. They are commonly used in low-level programming, data manipulation, and certain optimization tasks. JavaScript provides several bitwise operators that allow you to manipulate the binary representations of numbers. Here are the main bitwise operators along with explanations:
Bitwise AND (&): Performs a bitwise AND operation on each pair of corresponding bits. If both bits are 1, the result is 1; otherwise, it’s 0.
let result = 5 & 3; // Binary: 0101 & 0011 = 0001 (1)
Bitwise OR (|): Performs a bitwise OR operation on each pair of corresponding bits. If at least one of the bits is 1, the result is 1; otherwise, it’s 0.
let result = 5 | 3; // Binary: 0101 | 0011 = 0111 (7)
Bitwise XOR (^): Performs a bitwise XOR (exclusive OR) operation on each pair of corresponding bits. If the bits are different (01 or 10), the result is 1; if the bits are the same (00 or 11), the result is 0.
let result = 5 ^ 3; // Binary: 0101 ^ 0011 = 0110 (6)
Bitwise NOT (~): Inverts the bits of its operand, changing 0 to 1 and vice versa. The result is a two’s complement representation.
let result = ~5; // Binary: ~0101 = 1010 (-6 in two's complement)
Left Shift (<<): Shifts the bits of the left operand to the left by the number of positions specified by the right operand. Filling bits are shifted out, and zeros are shifted in.
let result = 5 << 1; // Binary: 0101 << 1 = 1010 (10) Right Shift (>>):
Shifts the bits of the left operand to the right by the number of positions specified by the right operand. Filling bits depend on the sign bit (arithmetic shift).
let result = 5 >> 1; // Binary: 0101 >> 1 = 0010 (2)
Zero-fill Right Shift (>>>):
Similar to the right shift operator, but it fills in zeros from the left instead of replicating the sign bit.
let result = -5 >>> 1; // Binary: -101 >>> 1 = 11111111111111111111111111111110 (4294967295)
Type Operators:
Type operators in JavaScript are used to determine the data type of a value or to check whether an object is an instance of a particular class or constructor. There are two main type operators in JavaScript: typeof and instanceof. Here’s an explanation of each:
typeof Operator: The typeof operator is used to determine the data type of a value or expression. It returns a string indicating the data type of the operand. Common return values include “number”, “string”, “boolean”, “object”, “function”, and “undefined”.
console.log(typeof 42); // "number"
console.log(typeof 'Hello'); // "string"
console.log(typeof true); // "boolean"
console.log(typeof { prop: 1 }); // "object"
console.log(typeof function() {}); // "function"
console.log(typeof undefined); // "undefined"
instanceof Operator:
The instanceof operator is used to check if an object is an instance of a particular class or constructor. It returns true if the object is an instance of the specified class, and false otherwise.
class Person {}
let person = new Person();
console.log(person instanceof Person); // true
console.log(person instanceof Object); // true (all objects are instances of Object)
The typeof operator is commonly used to perform basic type checking, especially when dealing with primitive values. The instanceof operator is used to check object instances and inheritance relationships, making it useful when working with custom classes and prototypes.