0% found this document useful (0 votes)
6 views7 pages

Essential JavaScript Operators Guide

Uploaded by

akbar111birbal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Essential JavaScript Operators Guide

Uploaded by

akbar111birbal
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations like addition,
subtraction, multiplication, and division.

Example Operators:

+ (Addition)

- (Subtraction)

* (Multiplication)

/ (Division)

% (Modulus)

<!DOCTYPE html>

<html>

<head>

<title>Arithmetic Operators</title>

</head>

<body>

<h3>Arithmetic Operators Example</h3>

<script>

let a = 10;

let b = 3;

let addition = a + b;

let subtraction = a - b;

let multiplication = a * b;

let division = a / b;

let modulus = a % b;
[Link]("Addition (10 + 3) = " + addition + "<br>");

[Link]("Subtraction (10 - 3) = " + subtraction + "<br>");

[Link]("Multiplication (10 * 3) = " + multiplication + "<br>");

[Link]("Division (10 / 3) = " + division + "<br>");

[Link]("Modulus (10 % 3) = " + modulus + "<br>");

</script>

</body>

</html>

2. Comparison Operators

Comparison operators compare two values and return a boolean (true or false).

Example Operators:

== (Equal to)

!= (Not equal to)

=== (Strict equal to)

!== (Strict not equal to)

> (Greater than)

< (Less than)

>= (Greater than or equal to)

<= (Less than or equal to)

<!DOCTYPE html>

<html>

<head>
<title>Comparison Operators</title>

</head>

<body>

<h3>Comparison Operators Example</h3>

<script>

let x = 5;

let y = "5";

let result1 = (x == y); // true, because values are equal

let result2 = (x === y); // false, because types are different

let result3 = (x != y); // false, because values are equal

let result4 = (x !== y); // true, because types are different

let result5 = (x > 3); // true

let result6 = (x < 10); // true

[Link]("5 == '5': " + result1 + "<br>");

[Link]("5 === '5': " + result2 + "<br>");

[Link]("5 != '5': " + result3 + "<br>");

[Link]("5 !== '5': " + result4 + "<br>");

[Link]("5 > 3: " + result5 + "<br>");

[Link]("5 < 10: " + result6 + "<br>");

</script>

</body>

</html>
3. Logical Operators

Logical operators are used to combine two or more conditions.

Example Operators:

&& (Logical AND)

|| (Logical OR)

! (Logical NOT)

<!DOCTYPE html>

<html>

<head>

<title>Logical Operators</title>

</head>

<body>

<h3>Logical Operators Example</h3>

<script>

let a = true;

let b = false;

let andResult = a && b; // false, because both are not true

let orResult = a || b; // true, because at least one is true

let notResult = !a; // false, because a is true

[Link]("true && false: " + andResult + "<br>");

[Link]("true || false: " + orResult + "<br>");

[Link]("!true: " + notResult + "<br>");

</script>

</body>
</html>

4. Assignment Operators

Assignment operators are used to assign values to variables.

Example Operators:

= (Assign)

+= (Add and assign)

-= (Subtract and assign)

*= (Multiply and assign)

/= (Divide and assign)

%= (Modulus and assign)

5. Increment and Decrement Operators

These operators are used to increase or decrease a variable's value by 1.

Example Operators:

++ (Increment)

-- (Decrement)

<!DOCTYPE html>

<html>

<head>

<title>Increment and Decrement Operators</title>

</head>

<body>

<h3>Increment and Decrement Operators Example</h3>

<script>
let count = 5;

count++; // Increment by 1

let incrementResult = count; // 6

count--; // Decrement by 1

let decrementResult = count; // 5

[Link]("After increment: " + incrementResult + "<br>");

[Link]("After decrement: " + decrementResult + "<br>");

</script>

</body>

</html>

String Operators

The + operator can also be used to concatenate (join) strings.

HTML Example:

<!DOCTYPE html>

<html>

<head>

<title>String Operators</title>

</head>

<body>

<h3>String Operators Example</h3>

<script>

let greeting = "Hello, ";


let name = "Alice";

let message = greeting + name; // Concatenates the two strings

[Link]("Greeting message: " + message + "<br>");

</script>

</body>

</html>

Common questions

Powered by AI

Comparison operators form logical assertions within control flow statements by enabling evaluation of conditions that guide code execution paths. In conditional statements like if, else if, and switch, these operators determine the boolean outcome of expressions (e.g., x > y), which dictates whether certain blocks of code will run based on whether conditions meet specified criteria . This is foundational in decision-making structures in programming where different execution paths depend on contrasting values.

A programmer uses logical NOT (!) in a conditional statement to reverse or negate the truth value of a condition. This is crucial when the logic requires executing a block of code only if a certain condition is false. By applying the NOT operator, the boolean evaluation of the condition changes, facilitating scenarios like checking for non-existence or 'not met' requirements (e.g., !hasLoggedIn), effectively transforming positive checks into negative conditions and vice versa .

In JavaScript, string concatenation is performed using the + operator, which joins two strings end-to-end. For example, if variable 'greeting' holds 'Hello, ' and variable 'name' holds 'Alice', the expression greeting + name results in 'Hello, Alice' . This is commonly used to construct dynamic messages or assemble strings from parts, such as generating a greeting message by combining salutation with a name.

Increment (++) and decrement (--) operators are advantageous in control structures because they offer a concise way to update loop indices or counter variables, improving readability and reducing error-prone verbose statements. Within loops, these operators provide a shorthand mechanism for iteration, such as advancing an index on each iteration of a loop, which is crucial for accurate execution and performance in repeated operations .

Arithmetic division (/) in programming computes the quotient of two numbers, essentially the result of distributing the division evenly, and often returns a floating-point number when the division does not resolve evenly . In contrast, the modulus operation (%) returns only the remainder of that division, indicating what portion of the division is left over without distributing it into the result. For example, dividing 10 by 3 gives a quotient of 3.333... while the modulus operation 10 % 3 returns 1, showing the leftover after dividing evenly.

The strict equality operator (===) differs from the simple equality operator (==) by comparing both the value and the type of the operands. The simple equality operator (==) performs type coercion, allowing values of different types that hold the same value to be considered equal, such as 5 == '5', which evaluates to true . In contrast, 5 === '5' evaluates to false because the types are different, demonstrating that the strict equality operator checks both the value and the data type.

The modulus operator (%) is useful in scenarios where determining the remainder of an integer division is necessary. It returns the remainder of dividing one number by another. For instance, checking if a number is even or odd can be done using the modulus operator by evaluating if a number % 2 equals zero. It can also be used in cyclic or periodic processes, such as determining a day of the week in a calendar system or implementing round-robin scheduling .

Assignment operators such as +=, -=, *=, /=, and %= combine arithmetic operations with assignment, which makes code more concise by reducing redundancy. Instead of writing x = x + y, the operation can be streamlined to x += y. This reduces the complexity of the code by removing the repetition of the variable being modified and leverage automation of common arithmetic patterns . By simplifying code, it enhances maintainability and can reduce the likelihood of errors.

Logical operators, including && (Logical AND), || (Logical OR), and ! (Logical NOT), enhance decision making by allowing multiple conditions to be evaluated together, returning boolean results. An AND operator returns true only if both conditions are true; thus, it is used to ensure that multiple criteria are met. An OR operator returns true if at least one condition is true, which is useful for alternate criteria validation. The NOT operator negates a condition's boolean result, useful for toggling boolean states or implementing negation in logic .

Arithmetic operators, such as +, -, *, /, and %, are used to perform basic mathematical operations and return numerical results . Comparison operators, like ==, !=, ===, !==, >, <, >=, and <=, are used to compare two values and return a boolean value, true or false . While arithmetic operators calculate and output numerical values, comparison operators evaluate conditions and output boolean values.

You might also like