Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

Web Development - JavaScript Operators


JavaScript Operators

Operators are symbols that perform actions on values. You use them to calculate, compare, or assign data in your JavaScript programs.

In the example below, we use the + operator to add together two values:

Example

let sum = 10 + 2; // 12
Try it Yourself »

The + operator can also be used to add a variable and a value, or even two variables:

Example

let x = 10;
let y = 5;
let sum1 = x + 2; // adds a variable and a value
let sum2 = x + y; // adds two variables
Try it Yourself »

Types of Operators

JavaScript has several categories of operators:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • String Operators
  • Type Operators

Arithmetic Operators

Arithmetic operators perform math between numbers.

OperatorDescriptionExampleTry it
+Addition5 + 2Try it »
-Subtraction5 - 2Try it »
*Multiplication5 * 2Try it »
/Division10 / 2Try it »
%Modulus (Remainder)5 % 2Try it »
**Exponentiation3 ** 2Try it »
++Incrementx++Try it »
--Decrementx--Try it »

Example

Arithmetic in action:

let x = 5;
let y = 2;
let sum = x + y;
Try it Yourself »

Assignment Operators

Assignment operators assign values to variables, often combining assignment with another operation.

OperatorExampleSame AsTry it
=x = 5x = 5Try it »
+=x += 2x = x + 2Try it »
-=x -= 2x = x - 2Try it »
*=x *= 2x = x * 2Try it »
/=x /= 2x = x / 2Try it »
%=x %= 2x = x % 2Try it »

Example

Using assignment operators:

let x = 5;
x += 3; // same as x = x + 3
Try it Yourself »

Comparison Operators

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

OperatorDescriptionExampleTry it
==Equal to (loose)5 == "5"Try it »
===Strict equal (same type & value)5 === "5"Try it »
!=Not equal5 != 8Try it »
!==Strict not equal5 !== "5"Try it »
>Greater than8 > 5Try it »
<Less than3 < 5Try it »
>=Greater or equal5 >= 5Try it »
<=Less or equal2 <= 3Try it »

In the example below, JavaScript compares the value of age with 18. It checks if age is greater than or equal to 18:

Example

Comparing values:

let age = 18;
document.getElementById("demo").innerHTML = (age >= 18); // true
Try it Yourself »

You can also compare two variables. In the example below, JavaScript checks whether the value of a is greater than the value of b:

Example

Comparing two variables:

let a = 10;
let b = 15;
document.getElementById("demo").innerHTML = (a > b); // false
Try it Yourself »

Logical Operators

Logical operators are used to combine or modify Boolean values (true and false).

OperatorDescriptionExampleTry it
&&AND - true only if both are true(x > 5 && x < 10)Try it »
||OR - true if either is true(x < 5 || y > 10)Try it »
!NOT - reverses the result!(x == y)Try it »

Note: Logical operators return true or false depending on the result of the combined conditions.


Operator Precedence

Operator precedence decides which operations run first. Multiplication and division have higher precedence than addition and subtraction.

Example

Math order:

let result = 10 + 2 * 3; // 16 (2*3 runs first)
Try it Yourself »

Summary

  • Operators perform calculations, comparisons, and assignments.
  • Use arithmetic, comparison, logical, and string operators to manipulate data.
  • Operator precedence controls the order of evaluation.

Next, you will learn about JavaScript Booleans - how Boolean values control your program's flow..


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.