HTML CSS Bootstrap JavaScript jQuery MySQL PHP Data Mining

JavaScript Operators

JavaScript operators are special symbols that tell JavaScript to perform an action on values and variables. They are used to calculate numbers, assign values, compare data, join text, and make logical decisions inside a program.


Why Operators Matter in JavaScript

Without operators, JavaScript could store values but would not be very useful in real programs. Operators let you build expressions such as adding prices, comparing ages, updating counters, or checking whether two values are equal.

In simple terms, operators are the working tools that help JavaScript do something with the data you write.


A Simple Operator Example

Here is a basic example that uses the assignment operator = and the addition operator +:

let x = 5;
let y = 6;
let total = x + y;

console.log(total);

In this example:

  • = assigns values to variables
  • + adds x and y

Main Types of JavaScript Operators

JavaScript has many operators, but beginners usually start with these main groups:

Operator Type Purpose Examples
Arithmetic Perform math calculations +, -, *, /, %
Assignment Assign values to variables =, +=, -=
Comparison Compare two values ==, ===, >, <
Logical Combine or reverse conditions &&, ||, !
String Join text values +, +=

Arithmetic Operators

Arithmetic operators are used for mathematical work such as addition, subtraction, multiplication, and division.

let a = 10;
let b = 4;

console.log(a + b);
console.log(a - b);
console.log(a * b);
console.log(a / b);

These operators are commonly used in totals, prices, scores, percentages, and counters.


Assignment Operators

Assignment operators store values inside variables. The basic assignment operator is =, but JavaScript also supports shortcut assignment operators.

let points = 10;
points += 5;
points -= 2;

console.log(points);

Here, += adds and assigns in one step, while -= subtracts and assigns in one step.


Comparison Operators

Comparison operators check whether two values are equal, different, greater, or smaller. The result is always true or false.

let age = 18;

console.log(age >= 18);
console.log(age === 18);
console.log(age != 20);
Note: Comparison operators are often used with if statements, loops, and form validation because they help JavaScript make decisions.

Logical Operators

Logical operators work with conditions. They let you combine multiple checks or reverse a result.

let isLoggedIn = true;
let isAdmin = false;

console.log(isLoggedIn && isAdmin);
console.log(isLoggedIn || isAdmin);
console.log(!isAdmin);

These operators are useful when a decision depends on more than one condition.


String Operators

The plus operator + can also join strings together. This is called string concatenation.

let firstName = "Mim";
let lastName = "Akter";
let fullName = firstName + " " + lastName;

console.log(fullName);
Tip: The same operator can behave differently depending on the values it works with. With numbers, + adds. With strings, it joins text.

Operators and Expressions

Operators are usually part of expressions. An expression combines values, variables, and operators to produce a result.

let result = (5 + 3) * 2;

In this line, the operators work together inside one expression, and JavaScript produces the final value.


Key Points to Remember

  • Operators tell JavaScript what action to perform on values and variables
  • Arithmetic operators are used for calculations
  • Assignment operators store or update variable values
  • Comparison operators return true or false
  • Logical operators work with conditions and decisions
  • The + operator can add numbers or join strings