Operators

Operators in JavaScript are special symbols used to perform operations on variables and values.
They help programmers perform tasks like addition, subtraction, comparison, and logical checking.
Operators are one of the most important concepts in JavaScript programming and web development.

Types of JavaScript Operators

JavaScript provides several types of operators.

Operator Type Purpose
Arithmetic Operators Perform mathematical calculations
Assignment Operators Assign values to variables
Comparison Operators Compare two values
Logical Operators Combine multiple conditions

These operators help make programs smarter, faster, and more interactive.
Understanding JavaScript operators helps students build strong coding and problem-solving skills.

Operators are for Mathematical and Logical Computations

The Assignment Operator = assigns values

The Addition Operator + adds values

The Multiplication Operator * multiplies values

The Comparison Operator > compares values

Types of Operators in JavaScript

Operator Type Example
Arithmetic Operator +, -, *, /
Assignment Operator = , +=
Comparison Operator ==, >, <
Logical Operator &&, `

Explanation

  • Operators are used to perform calculations and comparisons in JavaScript.
  • Arithmetic operators help in mathematical operations like addition and subtraction.
  • Comparison operators help compare two values and return true or false.
  • Logical operators are used to combine multiple conditions.
  • Assignment operators help assign values to variables.
  • Operators are widely used in games, websites, calculators, and applications.
  • JavaScript operators improve decision-making and program functionality.

Real-Time Scenarios

  • Shopping websites use operators to calculate total prices and discounts.
  • School result systems use comparison operators to check pass or fail conditions.
  • Mobile apps use logical operators for login validation systems.
  • Banking applications use arithmetic operators for balance calculations.
  • Online games use operators to update scores and player levels.
  • Calculator apps use arithmetic operators for mathematical operations.
  • Social media platforms use comparison operators to verify user information.

Difference Between JavaScript Operators

Feature Arithmetic Operators Assignment Operators Comparison Operators Logical Operators
Purpose Perform calculations Assign values Compare values Combine conditions
Output Type Number Assigned Value Boolean (true/false) Boolean (true/false)
Examples +, -, *, / =, +=, -= ==, ===, > &&, `
Used For Mathematics Storing and updating values Decision making Multiple condition checking
Common Usage Calculator Apps Variable Management Result Systems Login Systems

Syntax

let result = 10 + 5;
let check = 10 > 5;
let output = true && false;

Program:

<!DOCTYPE html>
<html>

<head>
    <title>JavaScript Operators Example</title>
</head>

<body>

    <h1>JavaScript Operators Example</h1>

    <p id="output"></p>

    <script>

        let a = 20;
        let b = 10;

        // Arithmetic Operators
        let addition = a + b;
        let subtraction = a - b;
        let multiplication = a * b;
        let division = a / b;
        let modulus = a % b;

        // Assignment Operators
        let x = 5;
        x += 3; // x = x + 3

        // Comparison Operators
        let greaterThan = a > b;
        let equalTo = a == b;
        let strictEqual = a === b;

        // Logical Operators
        let andOperator = (a > 5 && b > 5);
        let orOperator = (a < 5 || b > 5);
        let notOperator = !(a > b);

        document.getElementById("output").innerHTML =
            "<h2>Arithmetic Operators</h2>" +
            "Addition (20 + 10): " + addition + "<br>" +
            "Subtraction (20 - 10): " + subtraction + "<br>" +
            "Multiplication (20 * 10): " + multiplication + "<br>" +
            "Division (20 / 10): " + division + "<br>" +
            "Modulus (20 % 10): " + modulus + "<br><br>" +

            "<h2>Assignment Operator</h2>" +
            "Value of x after x += 3: " + x + "<br><br>" +

            "<h2>Comparison Operators</h2>" +
            "20 > 10 : " + greaterThan + "<br>" +
            "20 == 10 : " + equalTo + "<br>" +
            "20 === 10 : " + strictEqual + "<br><br>" +

            "<h2>Logical Operators</h2>" +
            "(20 > 5 && 10 > 5) : " + andOperator + "<br>" +
            "(20 < 5 || 10 > 5) : " + orOperator + "<br>" +
            "!(20 > 10) : " + notOperator;

    </script>

</body>

</html>

Output

Arithmetic Operators

Addition (20 + 10): 30
Subtraction (20 - 10): 10
Multiplication (20 * 10): 200
Division (20 / 10): 2
Modulus (20 % 10): 0

Assignment Operator

Value of x after x += 3: 8

Comparison Operators

20 > 10 : true
20 == 10 : false
20 === 10 : false

Logical Operators

(20 > 5 && 10 > 5) : true
(20 < 5 || 10 > 5) : true
!(20 > 10) : false

Advantages of JavaScript Operators

  • Simplify calculations
  • Improve decision making
  • Reduce coding effort
  • Increase code efficiency
  • Make programs interactive
  • Enhance application functionality

Summary:

  • Operators help perform calculations and comparisons.
  • JavaScript supports multiple types of operators.
  • Operators are important for decision-making programs.
  • They improve program efficiency and logic building.
  • Operators are used in almost every JavaScript application.

Keywords

JavaScript Operators, Arithmetic Operators in JavaScript, Comparison Operators in JavaScript, Logical Operators in JavaScript, Assignment Operators in JavaScript, JavaScript Operator Types, JavaScript Syntax, JavaScript Examples, JavaScript Tutorial for Beginners, Learn JavaScript Programming, JavaScript Coding Examples, Frontend Development, JavaScript Interview Questions, Web Development Basics

Check your knowledge

Quickly verify what you've learned from this tutorial.

Question 1

What is the main purpose of operators in JavaScript?

Operators help perform mathematical and logical operations in JavaScript.

Question 2

Which operator is used for addition in JavaScript?

The plus (+) operator is used to add values in JavaScript.

Question 3

Which operator checks equality in JavaScript?

The double equal (==) operator is used to compare values.

Question 4

Which type of operator is && ?

The && operator is a logical operator used to combine conditions.

Question 5

What will be the output of 10 > 5 in JavaScript?

The comparison operator > checks values and returns true when the condition is correct.

Congratulations!

You've successfully mastered the knowledge check for "Operators."

Keep up the great work and continue your learning journey!

Previous Topic Variables and Data Types Next Topic Conditional Statements