JavaScript Basics

JavaScript Operators

JavaScript operators are symbols used to perform operations on values and variables.

Operators can be used for addition, subtraction, comparison, assignment, logical checks, and more.

Example

Example

javascript

let a = 10;
let b = 5;

let result = a + b;

Here, the + operator adds two values.

Why Operators are Used

Operators help JavaScript perform different actions.

For example:

Example

javascript

let total = 100 + 50;
let price = 500 - 100;
let result = 10 > 5;

In the above code:

CodeMeaning
100 + 50Adds two numbers
500 - 100Subtracts one number from another
10 > 5Checks if 10 is greater than 5

Operators are used in almost every JavaScript program.

Types of JavaScript Operators

JavaScript has different types of operators.

Operator TypeUse
Arithmetic OperatorsPerform mathematical calculations
Assignment OperatorsAssign values to variables
Comparison OperatorsCompare two values
Logical OperatorsCheck multiple conditions
String OperatorsJoin strings
Increment and Decrement OperatorsIncrease or decrease values

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations.

OperatorNameExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulusa % b
**Exponentiationa ** b

Example

javascript

let a = 10;
let b = 3;

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

The % operator returns the remainder after division.

Example

javascript

let result = 10 % 3;

Here, the result is 1 because 10 divided by 3 leaves a remainder of 1.

Assignment Operators

Assignment operators are used to assign values to variables.

OperatorExampleSame As
=x = 10x = 10
+=x += 5x = x + 5
-=x -= 5x = x - 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
%=x %= 5x = x % 5

Example

javascript

let x = 10;

x += 5;
console.log(x);

Here, x += 5 means x = x + 5.

Comparison Operators

Comparison operators are used to compare two values.

They return either true or false.

OperatorMeaningExample
==Equal to10 == "10"
===Equal value and equal type10 === 10
!=Not equal10 != 5
!==Not equal value or not equal type10 !== "10"
>Greater than10 > 5
<Less than5 < 10
>=Greater than or equal to10 >= 10
<=Less than or equal to5 <= 10

Example

javascript

let a = 10;
let b = 5;

console.log(a > b);
console.log(a < b);
console.log(a === b);

Use === instead of == in most cases because it checks both value and data type.

Example

javascript

console.log(10 == "10");
console.log(10 === "10");

== only compares values, but === compares value and type.

Logical Operators

Logical operators are used to check more than one condition.

OperatorNameMeaning
&&ANDReturns true if both conditions are true
||ORReturns true if at least one condition is true
!NOTReverses the result

Example

javascript

let age = 20;
let hasId = true;

console.log(age >= 18 && hasId);

Here, both conditions must be true.

Example with OR:

Example

javascript

let isEmailVerified = false;
let isPhoneVerified = true;

console.log(isEmailVerified || isPhoneVerified);

Here, the result is true if at least one condition is true.

Example with NOT:

Example

javascript

let isLoggedIn = false;

console.log(!isLoggedIn);

The ! operator reverses false into true.

String Operator

The + operator can also be used to join strings.

Example

javascript

let firstName = "Amit";
let lastName = "Kumar";

let fullName = firstName + " " + lastName;

Here, the + operator joins two strings.

You can also join strings with numbers.

Example

javascript

let message = "Age: " + 25;

JavaScript converts the number into a string and joins it with the text.

Increment Operator

The increment operator ++ increases a value by 1.

Example

javascript

let count = 5;

count++;
console.log(count);

Here, the value of count becomes 6.

Decrement Operator

The decrement operator -- decreases a value by 1.

Example

javascript

let count = 5;

count--;
console.log(count);

Here, the value of count becomes 4.

Complete Example

Complete Example

html

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript Operators</title>
</head>
<body>

  <h1>JavaScript Operators</h1>

  <p id="result"></p>

  <script>
    let a = 20;
    let b = 10;

    let addition = a + b;
    let subtraction = a - b;
    let multiplication = a * b;
    let division = a / b;

    let isGreater = a > b;
    let bothTrue = a > b && b > 5;

    document.getElementById("result").innerHTML =
      "Addition: " + addition + "<br>" +
      "Subtraction: " + subtraction + "<br>" +
      "Multiplication: " + multiplication + "<br>" +
      "Division: " + division + "<br>" +
      "Is A greater than B? " + isGreater + "<br>" +
      "Both conditions true? " + bothTrue;
  </script>

</body>
</html>

Try It Yourself

Run the above example in the Try It Editor.

Try changing the values of a and b.

Also try using different operators like %, ===, ||, ++, and --.

Important Points

  • Operators are used to perform operations on values and variables.
  • Arithmetic operators are used for mathematical calculations.
  • Assignment operators are used to assign or update values.
  • Comparison operators return true or false.
  • Logical operators are used to check multiple conditions.
  • The + operator can add numbers and also join strings.
  • Use === for strict comparison in JavaScript.

Conclusion

JavaScript operators are used to perform calculations, assign values, compare values, join strings, and check conditions.

Operators are a basic but very important part of JavaScript. Once you understand operators, you can write better expressions, conditions, calculations, and interactive programs.