DEV Community

swetha palani
swetha palani

Posted on

JavaScript in 2 Day – Operators

In JavaScript, operators are special symbols or keywords used to perform operations on values and variables. These are fundamental for building expressions and logic in your programs.

Types of Operators in JavaScript

1. Arithmetic   Perform mathematical operations +, -, *, /, %, **, ++, --
2. Assignment   Assign values to variables  =, +=, -=, *=, /=, %=, **=
3. Comparison   Compare two values  ==, !=, ===, !==, >, <, >=, <=
4. Logical  Combine or invert boolean values    &&, `
5. Bitwise  Perform bit-level operations    &, `
6. String   Concatenate strings +, +=
7. Ternary  Short-hand for if-else  condition ? expr1 : expr2
8. Type Check data type or delete variables typeof, instanceof, delete, void
Enter fullscreen mode Exit fullscreen mode

1.Arithmetic Operators

Arithmetic operators are symbols used in programming to perform common mathematical operations such as addition, subtraction, multiplication, and division on numerical values or variables.

let a = 10, b = 3;
console.log(a + b);  // 13
console.log(a % b);  // 1 (modulus)

Enter fullscreen mode Exit fullscreen mode

2.Assignment Operators

Assignment operators are used in programming to assign values to variables.
The most basic assignment operator is =, which assigns the value on the right to the variable on the left.

let x = 5;
x += 3;  // x = x + 3 → 8

Enter fullscreen mode Exit fullscreen mode

3.Comparison Operators

Comparison operators are used to compare two values or expressions.
They return a Boolean value: either true or false, depending on whether the comparison is correct.

console.log(5 == '5');   // true (loose equality)
console.log(5 === '5');  // false (strict equality)

Enter fullscreen mode Exit fullscreen mode

4.Logical Operators

Logical operators are used to combine two or more conditions (Boolean expressions).
They return a Boolean value (true or false) based on the logic of the conditions provided.

true && false;   // false
true || false;   // true
!true;           // false

Enter fullscreen mode Exit fullscreen mode

5.String Operators

String operators are used to perform operations on string values, such as combining, modifying, or comparing text.
These operators help in handling and manipulating strings in most programming languages.

let name = "Swetha" + " Palani"; // Swetha Palani

Enter fullscreen mode Exit fullscreen mode

TASK 1-Calculator

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Calculator Operations</title>
    <script>
        function add(num1, num2) {
            console.log("Addition of numbers:", num1 + num2);
        }

        function sub(num1, num2) {
            console.log("Subtraction of numbers:", num1 - num2);
        }

        function mul(num1, num2) {
            console.log("Multiplication of numbers:", num1 * num2);
        }

        function div(num1, num2) {
            console.log("Division of numbers:", num1 / num2);
        }

        function mod(num1, num2) {
            console.log("Modulus of numbers:", num1 % num2);
        }

        // Call the functions after declaring them
        add(5, 5);
        sub(5, 2);
        mul(3, 7);
        div(10, 2);
        mod(2, 2);
    </script>
</head>
<body>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

Top comments (0)